I have image which I have accessed from the image gallery. I want to store this image in the database and 开发者_如何学Pythonlater re size it and POST it to the server. Can any please tell me if I will loose the EXIF data if the image is compressed? My original image has the EXIF data. If the data is not lost. Please guide as to how I can compress and re size the image
- Image Resizing
Lot of answers in this area, for example: The simplest way to resize an UIImage? or more details at https://stackoverflow.com/questions/1282830/uiimagepickercontroller-uiimage-memory-and-more
- Image Compression
You can use UIImageJPEGRepresentation C function, which is declared as ...
NSData * UIImageJPEGRepresentation (
UIImage *image,
CGFloat compressionQuality
);
... you can pass UIImage object and set compression quality within range 0.0 (maximum compression) to 1.0 (best quality).
- EXIF loss
See answers at https://stackoverflow.com/questions/1282830/uiimagepickercontroller-uiimage-memory-and-more
if you want to compress image without losing exif, first you should get property of image,and the data UIImageJPEGRepresentation you get not contain exif.
1:getoritatinal Image data 2:compress providerData 3:wite exif to compress data
- (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];
}
精彩评论