Camera images are changing the orientation automatically in iphone - iphone

Im developing a photo app.
Here i'm using the images from both photo library and also the images taking through camera and displaying it in a image view.
The images from photo library is coming in correct orientation but camera images are displaying in different orientation.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:#"MyFolder"];
NSString *imagePath = [dataPath stringByAppendingPathComponent:#"girl.jpg"];
if ([[NSFileManager defaultManager] fileExistsAtPath:imagePath]) {
UIImage *image = [UIImage imageWithCGImage:[UIImage imageWithContentsOfFile:imagePath].CGImage scale:bieberImage.image.scale
orientation:NO];
// bieberImage.transform = CGAffineTransformMakeRotation(1.57079633);
CABasicAnimation *crossFade = [CABasicAnimation animationWithKeyPath:#"contents"];
crossFade.duration = 3.0;
crossFade.fromValue = (id)bieberImage.image.CGImage;
crossFade.toValue = (id)image.CGImage;
[self.bieberImage.layer addAnimation:crossFade forKey:#"animateContents"];
}
Can anyone tell me how can i correct this problem.
Thanks in advance.

Replace:
UIImage *image = [UIImage imageWithCGImage:[UIImage imageWithContentsOfFile:imagePath].CGImage scale:bieberImage.image.scale
orientation:NO];
With:
UIImage *image = [UIImage imageWithCGImage:[UIImage imageWithContentsOfFile:imagePath].CGImage scale:bieberImage.image.scale
orientation:UIImageOrientationRight];
Edit:
Or try this:
- (UIImage*) rotateImage:(UIImage* )src {
UIImageOrientation orientation = src.imageOrientation;
UIGraphicsBeginImageContext(src.size);
[src drawAtPoint:CGPointMake(0, 0)];
CGContextRef context = UIGraphicsGetCurrentContext();
if (orientation == UIImageOrientationRight) {
CGContextRotateCTM (context, [self radians:90]);
} else if (orientation == UIImageOrientationLeft) {
CGContextRotateCTM (context, [self radians:90]);
} else if (orientation == UIImageOrientationDown) {
// NOTHING
} else if (orientation == UIImageOrientationUp) {
CGContextRotateCTM (context, [self radians:0]);
}
return UIGraphicsGetImageFromCurrentImageContext();
}
- (CGFloat) radians:(int)degrees {
return (degrees/180)*(22/7);
}

-(UIImage *)update4:(UIImage *)image {
CGImageRef imageRef = [image CGImage];
CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(imageRef);
CGColorSpaceRef colorSpaceInfo = CGColorSpaceCreateDeviceRGB();
if (alphaInfo == kCGImageAlphaNone)
alphaInfo = kCGImageAlphaNoneSkipLast;
int width, height;
width = image.size.width;
height = image.size.height;
if(width>320)
{
width = 320;
height = height * 320/width;
}
if(height>480)
{
height = 480;
width = width * 480/height;
}
CGContextRef bitmap;
if (image.imageOrientation == UIImageOrientationUp | image.imageOrientation == UIImageOrientationDown) {
bitmap = CGBitmapContextCreate(NULL, width, height, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, alphaInfo);
} else {
bitmap = CGBitmapContextCreate(NULL, height, width, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, alphaInfo);
}
if (image.imageOrientation == UIImageOrientationLeft) {
//NSLog(#"image orientation left");
CGContextRotateCTM (bitmap, 3.14/180*90);
CGContextTranslateCTM (bitmap, 0, -height);
} else if (image.imageOrientation == UIImageOrientationRight) {
//NSLog(#"image orientation right");
CGContextRotateCTM (bitmap, -3.14/180*90);
CGContextTranslateCTM (bitmap, -width, 0);
} else if (image.imageOrientation == UIImageOrientationUp) {
//NSLog(#"image orientation up");
} else if (image.imageOrientation == UIImageOrientationDown) {
//NSLog(#"image orientation down");
CGContextTranslateCTM (bitmap, width,height);
CGContextRotateCTM (bitmap, -3.14/180*180);
}
CGContextDrawImage(bitmap, CGRectMake(0, 0, width, height), imageRef);
CGImageRef ref = CGBitmapContextCreateImage(bitmap);
UIImage *result = [UIImage imageWithCGImage:ref];
CGContextRelease(bitmap);
CGImageRelease(ref);
return result;
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
UIImage *updateImage = [self update4:image];
}

Related

Reasons for UIimagePickerControler make crash

-(IBAction)takePhoto:(id)sender
{
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
{
imgPicker = [[UIImagePickerController alloc] init];
imgPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imgPicker.delegate = self;
[self presentModalViewController:imgPicker animated:YES];
}
}
-(IBAction)btnCancelCamera_Clicked:(id)sender{
[self dismissModalViewControllerAnimated:YES];
}
-(IBAction)btnCaptureImage_Clicked:(id)sender{
[imgPicker takePicture];
}
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self savePhotoProcess:info];
}
-(void)savePhotoProcess:(NSDictionary *)imgObject
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSInteger appcnt = [defaults integerForKey:#"LaunchAmounts"];
pickedImage = [imgObject objectForKey:UIImagePickerControllerOriginalImage];
[self dismissModalViewControllerAnimated:YES];
pickedImage = [self scaleAndRotateImage:pickedImage];
NSData *imageData = UIImageJPEGRepresentation(pickedImage,0.7);
imageName = [NSString stringWithFormat:#"image%d.jpg",appcnt];
[imageArray addObject:imageName];
path = [SAVEDIMAGE_DIR stringByAppendingPathComponent:imageName];
[imageData writeToFile:path atomically:YES];
pickedImage=nil;
imageData=nil;
imgPicker=nil;
}
- (UIImage *)scaleAndRotateImage:(UIImage *)image
{
int width = image.size.width;
int height = image.size.height;
CGSize size = CGSizeMake(width, height);
CGRect imageRect;
if(image.imageOrientation==UIImageOrientationUp
|| image.imageOrientation==UIImageOrientationDown)
{
imageRect = CGRectMake(0, 0, width, height);
}
else
{
imageRect = CGRectMake(0, 0, height, width);
}
UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGContextTranslateCTM(context, 0, height);
CGContextScaleCTM(context, 1.0, -1.0);
if(image.imageOrientation==UIImageOrientationLeft)
{
CGContextRotateCTM(context, M_PI / 2);
CGContextTranslateCTM(context, 0, -width);
}
else if(image.imageOrientation==UIImageOrientationRight)
{
CGContextRotateCTM(context, - M_PI / 2);
CGContextTranslateCTM(context, -height, 0);
}
else if(image.imageOrientation==UIImageOrientationUp)
{
//DO NOTHING
}
else if(image.imageOrientation==UIImageOrientationDown)
{
CGContextTranslateCTM(context, width, height);
CGContextRotateCTM(context, M_PI);
}
CGContextDrawImage(context, imageRect, image.CGImage);
CGContextRestoreGState(context);
UIImage *img1 = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return (img1);
}
I have imagePickerController in my app.It is keep crashing when tested by client.I could not simultate that crash.please somebody tell what are the reasons that cause crash so i can simulate that problem.any help will be appreicated.thanks in advance.

Rotation of UIImage in iPhone

I have a problem
When I get an image from iPhone camera. i can get that in all orientations Like LeftLandscape, RightLandscape, Portrait and portrait upside down.
Now how can I rotate the image fetched from Camera Delegates to only one orientation i.e. Only in Portrait OR Landscape Right
Can anybody HElp ?
Try this...
CGSize size = sizeOfImage;
UIGraphicsBeginImageContext(size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextRotateCTM(ctx, angleInRadians);
CGContextDrawImage(UIGraphicsGetCurrentContext(),
CGRectMake(0,0,size.width, size.height),
image);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
Also refer this link...it will help you out...
http://www.platinumball.net/blog/2009/03/30/iphone-uiimage-rotation-and-mirroring/
NYXImagesKit: Handy UIImage Categories
By Benjamin Godard
This is a handy set of categories on UIImage for rotating, resizing, filtering, enhancing and more, in an efficient and easy-to-use manner. Might be useful in some of your apps!
Try this.It will be helpful
-(UIImage *)resizeImage:(UIImage *)image {
CGImageRef imageRef = [image CGImage];
CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(imageRef);
CGColorSpaceRef colorSpaceInfo = CGColorSpaceCreateDeviceRGB();
if (alphaInfo == kCGImageAlphaNone)
alphaInfo = kCGImageAlphaNoneSkipLast;
int width, height;
width = 640;//[image size].width;
height = 640;//[image size].height;
CGContextRef bitmap;
if (image.imageOrientation == UIImageOrientationUp | image.imageOrientation == UIImageOrientationDown) {
bitmap = CGBitmapContextCreate(NULL, width, height, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, alphaInfo);
} else {
bitmap = CGBitmapContextCreate(NULL, height, width, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, alphaInfo);
}
if (image.imageOrientation == UIImageOrientationLeft) {
NSLog(#"image orientation left");
CGContextRotateCTM (bitmap, radians(90));
CGContextTranslateCTM (bitmap, 0, -height);
} else if (image.imageOrientation == UIImageOrientationRight) {
NSLog(#"image orientation right");
CGContextRotateCTM (bitmap, radians(-90));
CGContextTranslateCTM (bitmap, -width, 0);
} else if (image.imageOrientation == UIImageOrientationUp) {
NSLog(#"image orientation up");
} else if (image.imageOrientation == UIImageOrientationDown) {
NSLog(#"image orientation down");
CGContextTranslateCTM (bitmap, width,height);
CGContextRotateCTM (bitmap, radians(-180.));
}
CGContextDrawImage(bitmap, CGRectMake(0, 0, width, height), imageRef);
CGImageRef ref = CGBitmapContextCreateImage(bitmap);
UIImage *result = [UIImage imageWithCGImage:ref];
CGContextRelease(bitmap);
CGImageRelease(ref);
return result;
}

Rotating UIImage give blank UIImage

I Want to rotate UIImage in 90 degrees with this method :
static inline double radians (double degrees) {return degrees * M_PI/180;}
UIImage* rotate(UIImage* src, UIImageOrientation orientation)
{
UIGraphicsBeginImageContext(src.size);
CGContextRef context = UIGraphicsGetCurrentContext();
if (orientation == UIImageOrientationRight) {
CGContextRotateCTM (context, radians(90));
} else if (orientation == UIImageOrientationLeft) {
CGContextRotateCTM (context, radians(-90));
} else if (orientation == UIImageOrientationDown) {
// NOTHING
} else if (orientation == UIImageOrientationUp) {
CGContextRotateCTM (context, radians(90));
}
[src drawAtPoint:CGPointMake(0, 0)];
return UIGraphicsGetImageFromCurrentImageContext();
}
the problem is that it give me a blank UIImage
Here is the code :-
UIImage *PortraitImage;
PortraitImage = [[UIImage alloc] initWithCGImage: currentImage.CGImage
scale: 1.0
orientation: UIImageOrientationLeft];
if you want to rotate UIImage at some particular angle then you can use CIIFilter to rotate at whatever angle you want but that is possible in IOS 5.0
CIImage *inputImage = [[CIImage alloc] initWithImage:[UIImage imageNamed:#"old-country-rain.jpg"]];
CIFilter *RotateFilter = [CIFilter filterWithName:#"CIStraightenFilter"];
[RotateFilter setValue:[NSNumber numberWithFloat: 1.04667f] forKey:#"inputAngle"];
[RotateFilter setValue:inputImage forKey:kCIInputImageKey];
CIImage *displayImage = RotateFilter.outputImage;
UIImage *finalImage = [UIImage imageWithCIImage:displayImage];

How to change the size of the crop rect when using UIImagePickerController

I need to change the size of the cropping rect when using UIImagePickerController. In my case, I need the user to select images that are 320x385 but the crop rect currently only allows 320x320 (when allow editing is on).
Any ideas?
I have done a similar project. You have to use an image controller. Above your image controller you have to add another image controller (just because this is your crop area)
aboveImageController's unfilled/empty part is where you will take. (so you should have an image with 320x480 sized and 320x385 empty in it.)
UIImage *image=theimageView.image;
CGSize newSize;
newSize.width=320;
newSize.height=385;
UIGraphicsBeginImageContext(newSize);
//x=0 (because widhth : 320) y=0 (aboveImageController's empty part's start)
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGImageRef imageRef = CGImageCreateWithImageInRect([newImage CGImage], rect);
UIImage *img = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
The dictionary in the delegate method returns all that:
NSString *const UIImagePickerControllerMediaType;
NSString *const UIImagePickerControllerOriginalImage;
NSString *const UIImagePickerControllerEditedImage;
NSString *const UIImagePickerControllerCropRect;
NSString *const UIImagePickerControllerMediaURL;
NSString *const UIImagePickerControllerReferenceURL;
NSString *const UIImagePickerControllerMediaMetadata;
I assume, you've been using the EditedImage which is pretty much useless ... maybe for some thumbnails ... anyway, the info dictionary contains CropRect data, only thing you have to do is to recalculate the sizes and crop the OriginalImage yourself, using for example this:
UIImage: Resize, then Crop
I haven't tested, this is just theory :) ... but in theory, this should work :D
After getting image from image picker and give crop functionality as different module. So user can select the part of image which one they want to crop.
You can use this
UIImage *image1 = [self resizeImage:image width:320 height:385];
-(UIImage *)resizeImage:(UIImage *)image width:(int)wdth height:(int)hght{
int w = image.size.width;
int h = image.size.height;
CGImageRef imageRef = [image CGImage];
int width, height;
int destWidth = wdth;
int destHeight = hght;
if(w > h){
width = destWidth;
height = h*destWidth/w;
}
else {
height = destHeight;
width = w*destHeight/h;
}
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef bitmap;
bitmap = CGBitmapContextCreate(NULL, width, height, 8, 4 * width, colorSpace, kCGImageAlphaPremultipliedFirst);
if (image.imageOrientation == UIImageOrientationLeft) {
CGContextRotateCTM (bitmap, M_PI/2);
CGContextTranslateCTM (bitmap, 0, -height);
} else if (image.imageOrientation == UIImageOrientationRight) {
CGContextRotateCTM (bitmap, -M_PI/2);
CGContextTranslateCTM (bitmap, -width, 0);
}
else if (image.imageOrientation == UIImageOrientationUp) {
} else if (image.imageOrientation == UIImageOrientationDown) {
CGContextTranslateCTM (bitmap, width,height);
CGContextRotateCTM (bitmap, -M_PI);
}
CGContextDrawImage(bitmap, CGRectMake(0, 0, width, height), imageRef);
CGImageRef ref = CGBitmapContextCreateImage(bitmap);
UIImage *result = [UIImage imageWithCGImage:ref];
CGContextRelease(bitmap);
CGImageRelease(ref);
return result;
}

Resizing UIimages pulled from the Camera also ROTATES the UIimage?

I am getting UIimages from the camera and assigning them to UIImageViews to be displayed. When I do this the camera gives me a 1200 x 1600 pixel image which I then assign to a UIImageView in my Application. The image is displayed as expected in the image view under this condition. However, when I attempt to RESIZE the retrieved UIImage before assigning it to the UIImageView, the image is resizing as expected but there IS a problem in that somewhere (in the RESIZING code?) my UIImage is getting ROTATED... As a result, when I assign the resized UIImage to a UIImageView the image is rotated 90 degrees and appears stretched as the aspect ratio (1200 x 1600 pixels) was unchanged...
I am using this to get a UIImage from the Camera:
- (void) imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
{
myImg = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
myResizedImg = [self resizeImage:myImg width:400 height:533];
[myImageView setImage:myResizedImg];
}
I am using this to resize it:
-(UIImage *)resizeImage:(UIImage *)anImage width:(int)width height:(int)height
{
CGImageRef imageRef = [anImage CGImage];
CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(imageRef);
if (alphaInfo == kCGImageAlphaNone)
alphaInfo = kCGImageAlphaNoneSkipLast;
CGContextRef bitmap = CGBitmapContextCreate(NULL, width, height, CGImageGetBitsPerComponent(imageRef), 4 * width, CGImageGetColorSpace(imageRef), alphaInfo);
CGContextDrawImage(bitmap, CGRectMake(0, 0, width, height), imageRef);
CGImageRef ref = CGBitmapContextCreateImage(bitmap);
UIImage *result = [UIImage imageWithCGImage:ref];
CGContextRelease(bitmap);
CGImageRelease(ref);
return result;
}
QUESTION: How do I RESIZE a UIImage pulled from the Camera WITHOUT rotating the pixels?
The reason your code doesn't work is because the imageOrientation on the code that you have is not being taken into account. Specifically, if the imageOrientation is right/left, then you need to both rotate the image and swap width/height. Here is some code to do this:
-(UIImage*)imageByScalingToSize:(CGSize)targetSize
{
UIImage* sourceImage = self;
CGFloat targetWidth = targetSize.width;
CGFloat targetHeight = targetSize.height;
CGImageRef imageRef = [sourceImage CGImage];
CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef);
CGColorSpaceRef colorSpaceInfo = CGImageGetColorSpace(imageRef);
if (bitmapInfo == kCGImageAlphaNone) {
bitmapInfo = kCGImageAlphaNoneSkipLast;
}
CGContextRef bitmap;
if (sourceImage.imageOrientation == UIImageOrientationUp || sourceImage.imageOrientation == UIImageOrientationDown) {
bitmap = CGBitmapContextCreate(NULL, targetWidth, targetHeight, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo);
} else {
bitmap = CGBitmapContextCreate(NULL, targetHeight, targetWidth, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo);
}
if (sourceImage.imageOrientation == UIImageOrientationLeft) {
CGContextRotateCTM (bitmap, radians(90));
CGContextTranslateCTM (bitmap, 0, -targetHeight);
} else if (sourceImage.imageOrientation == UIImageOrientationRight) {
CGContextRotateCTM (bitmap, radians(-90));
CGContextTranslateCTM (bitmap, -targetWidth, 0);
} else if (sourceImage.imageOrientation == UIImageOrientationUp) {
// NOTHING
} else if (sourceImage.imageOrientation == UIImageOrientationDown) {
CGContextTranslateCTM (bitmap, targetWidth, targetHeight);
CGContextRotateCTM (bitmap, radians(-180.));
}
CGContextDrawImage(bitmap, CGRectMake(0, 0, targetWidth, targetHeight), imageRef);
CGImageRef ref = CGBitmapContextCreateImage(bitmap);
UIImage* newImage = [UIImage imageWithCGImage:ref];
CGContextRelease(bitmap);
CGImageRelease(ref);
return newImage;
}
This will resize your image and rotate it to the correct orientation. If you need the definition for radians, it is:
static inline double radians (double degrees) {return degrees * M_PI/180;}
The answer Daniel gave is also correct, but it suffers from the problem that it is not thread-safe, since you're using UIGraphicsBeginImageContext(). Since the above code only uses CG functions, you're all set. I also have a similar function to resize and do proper aspect fill on images - let me know if that's what you're looking for.
Note: I got the original function from this post, and did some modifications to make it work on JPEGs.
I had this problem too with some of the code out there, i found this code that works, check it out, let me know what you find
+ (UIImage*)imageWithImage:(UIImage*)image
scaledToSize:(CGSize)newSize;
{
UIGraphicsBeginImageContext( newSize );
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
Swift 3
I add this to didFinishPickingMediaWithInfo method and then use image without worrying for its rotation.
var image = info[UIImagePickerControllerOriginalImage] as! UIImage
if (image.imageOrientation != .up) {
UIGraphicsBeginImageContextWithOptions(image.size, false, image.scale)
image.draw(in: CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height))
image = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
}
If you want to make a square image from a rectangle image (no matter horizontal or vertical) by cropping it by width or height in both sides, use my code. The difference is that I don't stretch, but I crop. I made it from the top code by modification:
//Cropping _image to fit it to the square by height or width
CGFloat a = _image.size.height;
CGFloat b = _image.size.width;
CGRect cropRect;
if (!(a==b)) {
if (a<b) {
cropRect = CGRectMake((b-a)/2.0, 0, a, a);
b = a;
}
else if (b<a) {
cropRect = CGRectMake(0, (a-b)/2.0, b, b);
a = b;
}
CGImageRef imageRef = CGImageCreateWithImageInRect([_image CGImage], cropRect);
UIImage* sourceImage = _image;
CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef);
CGColorSpaceRef colorSpaceInfo = CGImageGetColorSpace(imageRef);
CGContextRef bitmap;
if (sourceImage.imageOrientation == UIImageOrientationUp || sourceImage.imageOrientation == UIImageOrientationDown) {
bitmap = CGBitmapContextCreate(NULL, a, a, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo);
} else {
bitmap = CGBitmapContextCreate(NULL, a, a, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo);
}
if (sourceImage.imageOrientation == UIImageOrientationLeft) {
CGContextRotateCTM (bitmap, radians(90));
CGContextTranslateCTM (bitmap, 0, -a);
} else if (sourceImage.imageOrientation == UIImageOrientationRight) {
CGContextRotateCTM (bitmap, radians(-90));
CGContextTranslateCTM (bitmap, -a, 0);
} else if (sourceImage.imageOrientation == UIImageOrientationUp) {
// NOTHING
} else if (sourceImage.imageOrientation == UIImageOrientationDown) {
CGContextTranslateCTM (bitmap, a, a);
CGContextRotateCTM (bitmap, radians(-180.));
}
CGContextDrawImage(bitmap, CGRectMake(0, 0, a, a), imageRef);
CGImageRef ref = CGBitmapContextCreateImage(bitmap);
_image = [UIImage imageWithCGImage:ref];
CGImageRelease(imageRef);
}