Reputation: 43
My app has problems with memory management. I have used instruments and have noticed that allocation just builds itself up until it crashes. I am using ARC. When I open the app it starts with 97.13 mb memory. I want to see if this piece of code is efficient, if there is something that is causing the memory build up.
Update
According to Instruments when I open the app, the app is using 93.17mb. As I try to display the collection view(OthersViewController) multiple times memory gradually increases 1mb every time. After taking a picture(OthersCamera) and going back to the view controller, the memory flings up from 106.87mb to 158.38mb. AND strangely after this when I try to display the picture again(OthersViewController) the memory just flings up even more from 158.38 mb to 215.24mb. Recieving a low memory warning and crash.
Github:https://github.com/canoneoskiss/name/tree/master
Upvotes: 0
Views: 117
Reputation: 104092
I looked at your code, and your storyboard is a big mess. Your problem is due to the way you move between controllers. You're going backwards in the storyboard with segues, and you shouldn't do that. When you go backwards with a segue, you're not going back to the controller you came from, you're instantiating a new instance of that controller. Except for unwind segues, segues ALWAYS instantiate new controllers. Since the presenting controller and the presented controller in a modal segue have strong pointers to each other, none of these controllers ever gets deallocated.
To fix this, you need to redo your storyboard. Either use unwind segues to go backwards, or dismiss any modal controllers in code (no segue) with dismissViewControllerAnimated:completion:.
Upvotes: 0
Reputation: 1097
After capturing image change its pixels because in iPhone 1 image will be @ 8MB. Even i faced this issue. use below code. after capturing image send it to this method it will return a low memory image.
+(UIImage *)croppIngimageByImageName:(UIImage *)imageToCrop toRect:(CGRect)rect forImageViewRect:(CGSize)imageVwRect{
// UIImage *tempImage;
// tempImage=[self scaleImage:imageToCrop toSize:imageVwRect];
CGImageRef imageRef = CGImageCreateWithImageInRect([imageToCrop CGImage], rect);
UIImage *cropped = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
// cropped=[self scaleImage:cropped toSize:rect.size];
return cropped;
}
or
+(UIImage *)scaleAndRotateImage:(UIImage *)image size:(int)maxSize {
// int kMaxResolution = 640; // Or whatever
int kMaxResolution = maxSize; // Or whatever
CGImageRef imgRef = image.CGImage;
CGFloat width = CGImageGetWidth(imgRef);
CGFloat height = CGImageGetHeight(imgRef);
CGAffineTransform transform = CGAffineTransformIdentity;
CGRect bounds = CGRectMake(0, 0, width, height);
if (width > kMaxResolution || height > kMaxResolution) {
CGFloat ratio = width/height;
if (ratio > 1) {
bounds.size.width = kMaxResolution;
bounds.size.height = roundf(bounds.size.width / ratio);
}
else {
bounds.size.height = kMaxResolution;
bounds.size.width = roundf(bounds.size.height * ratio);
}
}
CGFloat scaleRatio = bounds.size.width / width;
CGSize imageSize = CGSizeMake(CGImageGetWidth(imgRef), CGImageGetHeight(imgRef));
CGFloat boundHeight;
UIImageOrientation orient = image.imageOrientation;
switch(orient) {
case UIImageOrientationUp: //EXIF = 1
transform = CGAffineTransformIdentity;
break;
case UIImageOrientationUpMirrored: //EXIF = 2
transform = CGAffineTransformMakeTranslation(imageSize.width, 0.0);
transform = CGAffineTransformScale(transform, -1.0, 1.0);
break;
case UIImageOrientationDown: //EXIF = 3
transform = CGAffineTransformMakeTranslation(imageSize.width, imageSize.height);
transform = CGAffineTransformRotate(transform, M_PI);
break;
case UIImageOrientationDownMirrored: //EXIF = 4
transform = CGAffineTransformMakeTranslation(0.0, imageSize.height);
transform = CGAffineTransformScale(transform, 1.0, -1.0);
break;
case UIImageOrientationLeftMirrored: //EXIF = 5
boundHeight = bounds.size.height;
bounds.size.height = bounds.size.width;
bounds.size.width = boundHeight;
transform = CGAffineTransformMakeTranslation(imageSize.height, imageSize.width);
transform = CGAffineTransformScale(transform, -1.0, 1.0);
transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);
break;
case UIImageOrientationLeft: //EXIF = 6
boundHeight = bounds.size.height;
bounds.size.height = bounds.size.width;
bounds.size.width = boundHeight;
transform = CGAffineTransformMakeTranslation(0.0, imageSize.width);
transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);
break;
case UIImageOrientationRightMirrored: //EXIF = 7
boundHeight = bounds.size.height;
bounds.size.height = bounds.size.width;
bounds.size.width = boundHeight;
transform = CGAffineTransformMakeScale(-1.0, 1.0);
transform = CGAffineTransformRotate(transform, M_PI / 2.0);
break;
case UIImageOrientationRight: //EXIF = 8
boundHeight = bounds.size.height;
bounds.size.height = bounds.size.width;
bounds.size.width = boundHeight;
transform = CGAffineTransformMakeTranslation(imageSize.height, 0.0);
transform = CGAffineTransformRotate(transform, M_PI / 2.0);
break;
default:
[NSException raise:NSInternalInconsistencyException format:@"Invalid image orientation"];
}
UIGraphicsBeginImageContext(bounds.size);
CGContextRef context = UIGraphicsGetCurrentContext();
if (orient == UIImageOrientationRight || orient == UIImageOrientationLeft) {
CGContextScaleCTM(context, -scaleRatio, scaleRatio);
CGContextTranslateCTM(context, -height, 0);
}
else {
CGContextScaleCTM(context, scaleRatio, -scaleRatio);
CGContextTranslateCTM(context, 0, -height);
}
CGContextConcatCTM(context, transform);
CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, width, height), imgRef);
UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return imageCopy;
}
Upvotes: 0