PHAsset *phAsset = asset;
PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
//ptions.deliveryMode = PHImageRequestOptionsDeliveryModeFastFormat;
//是保证 resultHandler 值回调一次,不然可能回回调屡次,只有最后一次返回的图片大小等于设置的 targetSize
options.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
options.resizeMode = PHImageRequestOptionsResizeModeExact;
options.synchronous = YES;
options.networkAccessAllowed = YES;
CGSize imageSize = [UIImage getImageSizeWithAsset:phAsset];
[[PHImageManager defaultManager] requestImageForAsset:phAsset targetSize:imageSize contentMode:PHImageContentModeAspectFill options:options resultHandler:^(UIImage *result, NSDictionary *info) {
NSMutableData *imageData = [UIImage writeMetaDataWithData:result asset:phAsset];
[imageData writeToFile:imagePath atomically:YES];
[[UIImage getSmallImageWithImage:result] writeToFile:smallImagePath atomically:YES];
}];
复制代码
常规使用状况下deliveryMode 使用PHImageRequestOptionsDeliveryModeFastFormat 在iOS 13时候调用requestImageForAsset会出现result是一张小图(缩略图)bash
能够经过修改为PHImageRequestOptionsDeliveryModeHighQualityFormat返回设置targetSize对应的图片ui
这个坑找了不少资料才找到(iOS小菜鸟就这样)atom
+ (NSMutableData *)writeMetaDataWithData:(NSMutableData *)imageData metaDataDic:(NSDictionary *)metaDataDic {
NSMutableData *mutableData = [NSMutableData data];
CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)imageData, NULL);
CGImageDestinationRef destination = CGImageDestinationCreateWithData(
(__bridge CFMutableDataRef)mutableData, CGImageSourceGetType(cgImage), 1, nil);
CGImageDestinationAddImageFromSource(destination, source, 0, (__bridge CFDictionaryRef)metaDataDic);
CGImageDestinationFinalize(destination);
CFRelease(source);
CFRelease(destination);
return mutableData;
// CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)imageData, NULL);
// CFStringRef UTI = CGImageSourceGetType(source);
// CGImageDestinationRef destination = CGImageDestinationCreateWithData((__bridge CFMutableDataRef)imageData, UTI, 1, NULL);
// CGImageDestinationAddImageFromSource(destination, source, 0, (__bridge CFDictionaryRef)metaDataDic);
// CGImageDestinationFinalize(destination);
// return imageData;
}
复制代码
上面的方法使用注释的代码,在iOS 13上面会致使imageData 被清空为0byte,而后使用新建立一个NSMutableData 却没有这个问题,不知道是啥状况,有知道的iOS大佬能够留言,求告知一下spa