[UIImage imageWithData:data]数据变大问题,求解答

    NSData *data = [image compressWithMaxLength:200*1024];
    NSLog(@"Before compressing quality, image size ==== = %ld KB",data.length/1024);
    UIImage *resultImage = [UIImage imageWithData:data];
    NSData *data2 =  UIImageJPEGRepresentation(resultImage,1);
    NSLog(@"Before compressing quality, image size ====== %ld KB",data2.length/1024);
    [_model.images addObject:resultImage];
    NSData *data3= UIImageJPEGRepresentation(_model.images[0],1);
    NSLog(@"Before compressing quality, image size ====== %ld KB",data3.length/1024);

输出:

2019-05-16 21:48:47.610914+0800 CommunityCloud[7014:1699849] Before compressing quality, image size == 199 KB

2019-05-16 21:48:47.717712+0800 CommunityCloud[7014:1699849] Before compressing quality, image size == 968 KB

2019-05-16 21:48:47.821725+0800 CommunityCloud[7014:1699849] Before compressing quality, image size == 968 KB

 

无语。。。

可以使用 `ImageIO` 框架中的函数来将一个 `UIImage` 对象的 Exif 信息覆盖到另一个 `UIImage` 对象中。以下是实现的步骤: 1. 获取第一个 `UIImage` 对象的 Exif 信息。 2. 创建一个可变的 `NSMutableDictionary` 对象,用于存储 Exif 信息。 3. 将第一个 `UIImage` 对象的 Exif 信息存储到 `NSMutableDictionary` 中。 4. 创建一个 `CGImageSourceRef` 对象,用于读取第二个 `UIImage` 对象的数据。 5. 创建一个 `CGImageDestinationRef` 对象,用于将修改后的数据写入到新的 `UIImage` 对象中。 6. 将第二个 `UIImage` 对象的数据写入到 `CGImageSourceRef` 中。 7. 设置 `CGImageDestinationRef` 对象的属性,包括输出格式和 Exif 信息。 8. 使用 `CGImageDestinationFinalize` 函数将修改后的数据写入到新的 `UIImage` 对象中。 以下是代码示例: ``` UIImage *aImage = [UIImage imageNamed:@"aImage.jpg"]; UIImage *bImage = [UIImage imageNamed:@"bImage.jpg"]; // 获取第一个图像的 Exif 信息 NSMutableDictionary *exifData = [NSMutableDictionary dictionary]; NSDictionary *metadata = aImage.imageMetadata; NSDictionary *exif = metadata[(NSString *)kCGImagePropertyExifDictionary]; [exifData addEntriesFromDictionary:exif]; // 创建 CGImageSourceRef 对象 NSData *bImageData = UIImageJPEGRepresentation(bImage, 1.0); CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)bImageData, NULL); // 创建 CGImageDestinationRef 对象 NSMutableData *mutableData = [NSMutableData data]; CGImageDestinationRef destination = CGImageDestinationCreateWithData((__bridge CFMutableDataRef)mutableData, kUTTypeJPEG, 1, NULL); // 设置输出属性和 Exif 信息 NSMutableDictionary *options = [NSMutableDictionary dictionary]; [options setObject:@(1.0) forKey:(NSString *)kCGImageDestinationLossyCompressionQuality]; [options setObject:exifData forKey:(NSString *)kCGImagePropertyExifDictionary]; CGImageDestinationSetProperties(destination, (__bridge CFDictionaryRef)options); // 将第二个图像的数据写入到 CGImageDestinationRef 中 CGImageDestinationAddImageFromSource(destination, source, 0, (__bridge CFDictionaryRef)options); // 将修改后的数据写入到新的 UIImage 对象中 CGImageDestinationFinalize(destination); UIImage *newImage = [UIImage imageWithData:mutableData]; // 释放资源 CFRelease(source); CFRelease(destination); ``` 注意,上述代码中的 `aImage` 和 `bImage` 应该是尺寸相同的图片,否则需要进行相应的缩放或裁剪操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

路途遥远gg

帮到你了就好

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值