Reputation: 57060
I have a strange issue on iOS7.
The user can open an image in the Photos app and crop it.
In my app, when I attempt to get the image, it comes back without the crop. Other edits, such as rotation, are preserved.
I present the image picker without editing capabilities.
My code:
ALAssetsLibrary *assetLibrary=[[ALAssetsLibrary alloc] init];
[assetLibrary assetForURL:[info valueForKey:UIImagePickerControllerReferenceURL] resultBlock:^(ALAsset *asset) {
ALAssetRepresentation *rep = [asset defaultRepresentation];
Byte *buffer = (Byte*)malloc(rep.size);
NSUInteger buffered = [rep getBytes:buffer fromOffset:0.0 length:rep.size error:nil];
NSData *data = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES];//this is NSData may be what you want
editedImage = [UIImage imageWithData:data];
} failureBlock:^(NSError *err) {
NSLog(@"Error: %@",[err localizedDescription]);
}];
If I use
UIImage *image = info[UIImagePickerControllerOriginalImage];
content = UIImageJPEGRepresentation(image, 1.0);
the image is cropped correctly.
Is this a bug in iOS7?
Upvotes: 0
Views: 289
Reputation: 57060
It turns out, the crop is actually saved in a metadata.
Here is what I ended up doing:
ALAssetRepresentation *rep = [asset defaultRepresentation];
NSString *xmpString = rep.metadata[@"AdjustmentXMP"];
if(xmpString)
{
NSData *xmpData = [xmpString dataUsingEncoding:NSUTF8StringEncoding];
CIImage *image = [CIImage imageWithCGImage:rep.fullResolutionImage];
NSError *error = nil;
NSArray *filterArray = [CIFilter filterArrayFromSerializedXMP:xmpData
inputImageExtent:image.extent
error:&error];
if (error)
{
DM_FLOG(@"ERROR:: Error during CIFilter creation: %@", [error localizedDescription]);
}
for (CIFilter *filter in filterArray) {
[filter setValue:image forKey:kCIInputImageKey];
image = [filter outputImage];
}
CIContext* context = [CIContext contextWithOptions:nil];
CGImageRef cgImage = [context createCGImage:image fromRect:[image extent]];
context = nil;
image = nil;
contentType = @"image/jpeg";
content = UIImageJPEGRepresentation([UIImage imageWithCGImage:cgImage], 1.0);
CGImageRelease(cgImage);
}
else
{
Byte *buffer = (Byte*)malloc(rep.size);
NSUInteger buffered = [rep getBytes:buffer fromOffset:0.0 length:rep.size error:nil];
content = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES];
}
Upvotes: 1