检索图像内存使用情况

时间:2013-12-16 02:21:33

标签: ios memory

我的应用内存管理存在问题。我已经使用过仪器,并注意到分配只会自动构建,直到它崩溃。我正在使用ARC。当我打开应用程序时,它以97.13 MB的内存开始。我想知道这段代码是否有效,如果有什么东西导致内存积累。 的更新
据我打开应用程序时,该应用程序正在使用93.17mb。当我尝试多次显示集合视图(OthersViewController)时,内存逐渐增加1mb。在拍摄照片(OthersCamera)并返回视图控制器后,内存从106.87mb上升到158.38mb。奇怪的是,在此之后,当我再次尝试显示图片时(OthersViewController),内存刚刚从158.38 mb上升到215.24mb。收到低内存警告和崩溃。

Github上:https://github.com/canoneoskiss/name/tree/master

2 个答案:

答案 0 :(得分:1)

我看了你的代码,你的故事板很乱。您的问题是由于您在控制器之间移动的方式。你在故事板中用segues向后退,你不应该这样做。当您使用segue向后移动时,您不会返回到您来自的控制器,而是实例化该控制器的新实例。除了展开segue之外,segues总是实例化新的控制器。由于呈现控制器和模式segue中呈现的控制器具有彼此强的指针,因此这些控制器都不会被解除分配。

要解决此问题,您需要重做故事板。要么使用unwind segues向后移动,要么使用dismissViewControllerAnimated来解除代码中的任何模态控制器(没有segue):.完成:。

答案 1 :(得分:0)

捕获图像后更改其像素,因为在iPhone 1图像中将为@ 8MB。即使我遇到了这个问题。使用下面的代码。捕获图像后,将其发送到此方法,它将返回一个低内存图像。

+(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;

}

+(UIImage *)scaleAndRotateImage:(UIImage *)图像大小:(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;

}

相关问题