美文网首页
iOS 13图片上传相关适配

iOS 13图片上传相关适配

作者: lizubing1992 | 来源:发表于2019-10-24 14:29 被阅读0次

1.PHImageManager问题

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是一张小图(缩略图)

可以通过修改成PHImageRequestOptionsDeliveryModeHighQualityFormat返回设置targetSize对应的图片

这个坑找了很多资料才找到(iOS小菜鸟就这样)

2.CGImageDestinationAddImageFromSource问题

+ (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大佬可以留言,求告知一下

相关文章

网友评论

      本文标题:iOS 13图片上传相关适配

      本文链接:https://www.haomeiwen.com/subject/qyarvctx.html