从 iphone 的 照片库中选取的图片,由于系统不能返回其文件的具体路径,所以要用到 ALAssetsLibrary 这个框架
首先导入框架 ALAssetsLibrary
导入头文件 <ALAssetsLibrary/ALAssetsLibrary.h>
#pragma mark UIImagePickerController 的代理方法 进行自定义控制器的操作-----
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary*)info{
__weak typeof(self) weakself = self;
[picker dismissViewControllerAnimated:YES completion:^{
UIImage *imageOriginal = [info objectForKey:UIImagePickerControllerOriginalImage];
ALAssetsLibrary* alLibrary = [[ALAssetsLibrary alloc] init];
[alLibrary assetForURL:[info objectForKey:UIImagePickerControllerReferenceURL] resultBlock:^(ALAsset *asset) {
ALAssetRepresentation *representation = [asset defaultRepresentation];
NSString *fileSize = [NSString stringWithFormat:@"(%@)",[weakself getFileSizeByBt:[NSNumber numberWithLongLong:representation.size]]];
NSLog(@"#################%@##################",fileSize);
} failureBlock:^(NSError *error) {
}];.h
//计算图片大小
- (NSString *)getFileSizeByBt:(NSNumber *)fileSize{
CGFloat size = [fileSize floatValue];
if (size >= 1024*1024*1024) {
return [NSString stringWithFormat:@"%.2fG",size/(1024*1024*1024)];
}else if (size >= 1024*1024) {
return [NSString stringWithFormat:@"%.2fM",size/(1024*1024)];
}else{
return [NSString stringWithFormat:@"%.2fK",size/1024];
}
}