Could anybody help me with this issue I'm a bit new to objective c and iOS. I've been working on it but I can't figure out how to fix the problem, My app is really simple it only start the camera take pictures and send them through email to our server. This code was working just fine in iOS6.
When I take pictures my memory is heap growth with each screen capture and I get "Received Memory Warning" and finally - Terminated due to Memory Pressure. -
-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self.popoverController2 dismissPopoverAnimated:true];
NSString *mediaType = [info
objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
_image = [info
objectForKey:UIImagePickerControllerOriginalImage];
_image = [self fixrotation:_image]; //<----- increased memory when UIImageWriteToSavedPhotosAlbum is uncommented IF is comment it doesn't increased memory but after some pictures I start to get "Received Memory Warning" message until the app Crash.
if (_newMedia){
UIImageWriteToSavedPhotosAlbum(_image,
self,@selector(image:finishedSavingWithError:contextInfo:),
nil);
[self dismissViewControllerAnimated:NO completion:nil];
[self performSegueWithIdentifier:@"SeleccionadoCameraR" sender:self];
}else{
[self performSegueWithIdentifier:@"SeleccionadoCameraR" sender:self];
}
}
}
- (UIImage *)fixrotation:(UIImage *)image{
if (image.imageOrientation == UIImageOrientationUp) return image;
CGAffineTransform transform = CGAffineTransformIdentity;
switch (image.imageOrientation) {
case UIImageOrientationDown:
case UIImageOrientationDownMirrored:
transform = CGAffineTransformTranslate(transform, image.size.width, image.size.height);
transform = CGAffineTransformRotate(transform, M_PI);
break;
case UIImageOrientationLeft:
case UIImageOrientationLeftMirrored:
transform = CGAffineTransformTranslate(transform, image.size.width, 0);
transform = CGAffineTransformRotate(transform, M_PI_2);
break;
case UIImageOrientationRight:
case UIImageOrientationRightMirrored:
transform = CGAffineTransformTranslate(transform, 0, image.size.height);
transform = CGAffineTransformRotate(transform, -M_PI_2);
break;
case UIImageOrientationUp:
case UIImageOrientationUpMirrored:
break;
}
switch (image.imageOrientation) {
case UIImageOrientationUpMirrored:
case UIImageOrientationDownMirrored:
transform = CGAffineTransformTranslate(transform, image.size.width, 0);
transform = CGAffineTransformScale(transform, -1, 1);
break;
case UIImageOrientationLeftMirrored:
case UIImageOrientationRightMirrored:
transform = CGAffineTransformTranslate(transform, image.size.height, 0);
transform = CGAffineTransformScale(transform, -1, 1);
break;
case UIImageOrientationUp:
case UIImageOrientationDown:
case UIImageOrientationLeft:
case UIImageOrientationRight:
break;
}
// Now we draw the underlying CGImage into a new context, applying the transform
// calculated above.
CGContextRef ctx = CGBitmapContextCreate(NULL, image.size.width, image.size.height,
CGImageGetBitsPerComponent(image.CGImage), 0,
CGImageGetColorSpace(image.CGImage),
CGImageGetBitmapInfo(image.CGImage));
CGContextConcatCTM(ctx, transform);
switch (image.imageOrientation) {
case UIImageOrientationLeft:
case UIImageOrientationLeftMirrored:
case UIImageOrientationRight:
case UIImageOrientationRightMirrored:
// Grr...
CGContextDrawImage(ctx, CGRectMake(0,0,image.size.height,image.size.width), image.CGImage);
break;
default:
CGContextDrawImage(ctx, CGRectMake(0,0,image.size.width,image.size.height), image.CGImage); //when I use instruments it shows that My VM is because of this
break;
}
// And now we just create a new UIImage from the drawing context
CGImageRef cgimg = CGBitmapContextCreateImage(ctx);//also this line in Instruments
UIImage *img = [UIImage imageWithCGImage:cgimg];
CGContextRelease(ctx);
CGImageRelease(cgimg);
return img;
}
probably is a memory management. I will appreciate your help
UIImageWriteToSavedPhotosAlbum method use 30M+ memory on iOS7.It's not about your resize or fixrotation method.
I have fixed this issue by JRG-Developer-s answer up here with a little modifications: I `m using categories to fix orientation of image ans scaling it down before presenting and when done, I call Weak Self to assign this scaled and fixed image to my imageview)
and lower:
code of scaling was taken from the net:
and code for changing (fixing) image orientation was also taken from the net:
please, don-t be cruel in minusing if something looks dummy) I-m junior for now)
For answer below - that s why you would better scale down an image - to take less memory in use, but not simply save the big 4MB image to disc. At the very begining I also had memory issues - it was eating 30Mb per sigle photo - and I had to take 2 photos one by one... now it works fine and smooth. Fix orientation is optional, but I would recommend anyway to scale down photo - resize it to smaller.
You're on the right track with your
fixRotation
method. However, you should also resize the image. Otherwise, the image will be huge, ~30 MB (depending on device).Checkout this blog post on how to resize images correctly. Specifically, the
UIImage
category files you want are these:UIImage+Resize.h
UIImage+Resize.m
It's also a good idea to do this on a background thread. Something like this