iphone用exif数据减小图像的大小(以字节为单位)

时间:2011-02-16 13:24:02

标签: iphone post uiimage exif

我有从图库中访问过的图片。我想将此图像存储在数据库中,然后重新调整大小并将其POST到服务器。如果图像被压缩,是否可以告诉我是否会丢失EXIF数据?我的原始图片有EXIF数据。如果数据没有丢失。请指导我如何压缩和重新调整图像大小

2 个答案:

答案 0 :(得分:4)

  • 图片大小调整

此区域的答案很多,例如:The simplest way to resize an UIImage?https://stackoverflow.com/questions/1282830/uiimagepickercontroller-uiimage-memory-and-more处的更多详情

  • 图片压缩

您可以使用UIImageJPEGRepresentation C函数,该函数声明为...

NSData * UIImageJPEGRepresentation (
   UIImage *image,
   CGFloat compressionQuality
);

...您可以传递UIImage对象并将压缩质量设置在0.0(最大压缩)范围内,达到1.0(最佳质量)。

  • EXIF损失

查看https://stackoverflow.com/questions/1282830/uiimagepickercontroller-uiimage-memory-and-more

上的答案

答案 1 :(得分:0)

如果你想在不丢失exif的情况下压缩图像,首先你应该得到图像的属性,而你得到的数据UIImageJPEGRepresentation不包含exif。

1:getoritatinal图像数据  2:压缩providerData  3:wite exif压缩数据

- (void)setImagePropertyWith:(ALAsset *)asset

 {
        //get full imageData
        ALAssetRepresentation * defaultRep = [asset defaultRepresentation];
        Byte *buffer = (Byte*)malloc(defaultRep.size);
        NSUInteger buffered = [defaultRep getBytes:buffer fromOffset:0.0 length:defaultRep.size error:nil];
        NSData * data = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES];

        // compressData providerData 
        CGDataProviderRef jpegdata = CGDataProviderCreateWithCFData((CFDataRef)data);
        CGImageRef imageRef = CGImageCreateWithJPEGDataProvider(jpegdata, NULL, YES, kCGRenderingIntentDefault);
        UIImage * image = [UIImage imageWithCGImage:imageRef];
        NSData* finaldata = UIImageJPEGRepresentation(image, 0.5);

        //compressData with exif
        finaldata =  [self writeExif:(NSDictionary *)[self getPropertyOfdata:data] intoImage:finaldata];

    }
    - (CFDictionaryRef )getPropertyOfdata:(NSData *)data
    {
        CGImageSourceRef imageSource = CGImageSourceCreateWithData((CFDataRef)data, NULL);
        if (imageSource == NULL) {
            // Error loading image
            return nil;
        }
        NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                                 [NSNumber numberWithBool:NO], (NSString *)kCGImageSourceShouldCache,
                                 nil];
        CFDictionaryRef imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, (CFDictionaryRef)options);
        NSLog(@"ori:%@",imageProperties);
        return imageProperties;
    //    [self writeExif: (NSDictionary *)imageSource intoImageData:data];
    }