PHPhotoKit
2017-09-20 本文已影响24人
xiari1991
加载图片两种方式:
PHAsset *asset = self.phAsset;
PHImageRequestOptions *option = [[PHImageRequestOptions alloc] init];
option.synchronous = YES;
option.resizeMode = PHImageRequestOptionsResizeModeFast;
option.deliveryMode = PHImageRequestOptionsDeliveryModeFastFormat;
方式1
[[PHImageManager defaultManager] requestImageDataForAsset:asset options:option resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
UIImage *image = [UIImage imageWithData:imageData];
originImage = image;
NSLog(@"imageSize: %@", NSStringFromCGSize(image.size));
}];
方式2
// 从asset中获得图片
CGSize size = CGSizeMake(asset.pixelWidth, asset.pixelHeight);
[[PHImageManager defaultManager] requestImageForAsset:asset targetSize:[ImageAssetsModel maxImageSize:size] contentMode:PHImageContentModeAspectFill options:option resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
//NSLog(@"asset_pixW :%zd, asset_pixH: %zd; imageSize: %@", asset.pixelWidth, asset.pixelHeight, NSStringFromCGSize(result.size));
BOOL downloadFinined = (![[info objectForKey:PHImageCancelledKey] boolValue] && ![info objectForKey:PHImageErrorKey]);
if (downloadFinined && result != nil) {
// info 图片大小不同 里面的键也不同 有些会缺失
// 调整图片方向
ImageCropperController *imageCorpper = [[ImageCropperController alloc] init];
result = [imageCorpper fixOrientation:result];
originImage = result;
//下面两个中任一个都可以标示相册中图片的唯一性
originImage.localIdentifier = asset.localIdentifier;
originImage.phImageFileURLKey = info[@"PHImageFileURLKey"];
}
}];
当同时加载多张原图(大图)的时候,方式2如果target设置成原图大小,那么加载过程会很慢,并且内存瞬间暴涨。但使用方式1则会解决这个问题,速度很快,并且内存不会很大。