UIImage copy ends up rotated
I am trying to copy a uiimage (ultimately I am planning to blur the copy). Right now the copied image shows correctly in the simulator but ends up rotated 90 degrees when running on the device.
I am using the following to create acopy:
CGImageRef cgImage = [sourceImage CGImage];
// Make a new image fr开发者_开发技巧om the CG Reference UIImage *copyOfImage = [[UIImage alloc] initWithCGImage:cgImage];
The source image is coming from a UIImagePickerController. The same results happen whether I grab the soruce from the library or the camera.
Thanks in advance for any help
According to documentation for UIImage, imageOrientation property contain the orientation for a image and it defaults to UIImageOrientationUp. But if the image have any EXIF data for orientation it will return that instead.
imageWithCGImage:(CGImageRef)cgImage and initWithCGImage:(CGImageRef)CGImage (in UIImage) will both default to UIImageOrientationUp orientation, which might be wrong if the image/movie actually was shot in e.g. landscape mode.
But if you use the versions of above that takes orientation as well and pass in sourceImage.imageOrientation it should work.
Having happened to have done this recently, particularly for use with blurring, I wouldn't suggest using a full image.
I found the process to be just way too slow, and since the original image is being blurred, you're better off making a small image, and then scaling it up and blurring it from there.
I haven't tested this since its a bit of a different intent than my original project.
This code uses UIImage+Resize for resizing.
//Creates an autoreleased representing the current screen view
-(UIImage *)currentBlurredImage:(UIImage *)anImage{
CGImageRef imageRef = CGImageCreateWithImageInRect([anImage CGImage], bounds);
UIImage *coppiedImage = [UIImage imageWithCGImage:imageRef scale:1.0 orientation:anImage.imageOrientation];
coppiedImage = [viewImage resizedImage:CGSizeMake(120, 80) interpolationQuality:kCGInterpolationLow];
coppiedImage = [viewImage blurredCopyUsingGuassFactor:4 andPixelRadius:4];
return coppiedImage ;
}
Blurring was done with the code from here http://iphonedevelopment.blogspot.com/2010/08/uiimage-blur.html (credit where its due!). However I cleaned it up and dealt with a bunch of memory, and other issues.
http://pastebin.com/MaHvvdim
精彩评论