正在检索图像内存使用情况



我的应用程序内存管理有问题。我使用过工具,并注意到分配只是建立起来,直到崩溃。我正在使用ARC。当我打开应用程序时,它的启动内存为97.13 mb。我想看看这段代码是否有效,是否有什么东西导致了内存的积累。更新
根据Instruments的数据,当我打开应用程序时,该应用程序使用了93.17mb。当我尝试多次显示集合视图(OthersViewController)时,内存每次逐渐增加1mb。在拍摄了一张照片(OthersCamera)并返回到视图控制器后,内存从106.87mb上升到158.38mb。奇怪的是,在这之后,当我试图再次显示图片(OthersViewController)时,内存从158.38 mb上升到215.24mb。收到内存不足警告并崩溃。

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

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

若要解决此问题,您需要重新制作故事板。使用展开分段后退,或者使用disseViewControllerAnimated:completion:来消除代码中的任何模态控制器(无分段)。

拍摄图像后更改其像素,因为在iPhone 1中,图像将为@8MB。就连我也面临这个问题。使用以下代码。在捕获图像后将其发送到此方法,它将返回低内存图像。

+(UIImage*)croppIngimageByImageName:

//  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;

}

最新更新