带图像比例的UIImage循环 - 接收内存警告

时间:2013-10-29 07:40:59

标签: objective-c memory-leaks uiimage

我有内存泄漏问题,我从1周后就找不到答案了。 :(

我在照片阵列中循环了20多张图像我总是收到内存警告。

也许有人可以帮我解决我令人沮丧的问题?

调试消息 “收到内存警告。”

在我的UIView中循环

images = [[NSMutableArray alloc] init];
for (Photo *photo in photos) {
    [images addObject:[[UIImage imageWithData:photo.image_compressed] ToSize:videoSize]];
}

ToSize方法

    #import "UIImage+toSize.h"

    @implementation UIImage (toSize)

    - (UIImage *)ToSize:(CGSize)newSize {

        float originalWidth = self.size.width;
        float originalHeight = self.size.height;

        float newWidth = newSize.width;
        float newHeight = newSize.height;

        float xMargin = 0.0f;
        float yMargin = 0.0f;

        float ratioWidth = (originalWidth / originalHeight) * newSize.height;
        float ratioHeight = (originalHeight / originalWidth) * newSize.width;

        // LEFT & RIGHT Margin
        if (ratioHeight > newSize.height)
        {
            // set new image size
            newWidth = ratioWidth;
            newHeight = newSize.height;

            // calculate margin
            xMargin = (newSize.width - ratioWidth) / 2;
        } else if (ratioWidth > newSize.width)
        {
            // set new image size
            newWidth = newSize.width;
            newHeight = ratioHeight;

            // calculate margin
            yMargin = (newSize.height - ratioHeight) / 2;
        }

        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
        CGContextRef ctx = CGBitmapContextCreate(NULL, newSize.width, newSize.height,
                                                 CGImageGetBitsPerComponent(self.CGImage), 0,
                                                 colorSpace,
                                                 CGImageGetBitmapInfo(self.CGImage));

        CGContextDrawImage(ctx, CGRectMake(xMargin, yMargin, newWidth, newHeight), self.CGImage);

        CGImageRef cgimg = CGBitmapContextCreateImage(ctx);

        UIImage *img = [UIImage imageWithCGImage:cgimg scale:self.scale orientation:UIImageOrientationUp];

        CGColorSpaceRelease(colorSpace);
        CGContextRelease(ctx);
        CGImageRelease(cgimg);

        return img;
    }

@end

1 个答案:

答案 0 :(得分:0)

“收到内存警告。”这不是内存泄漏,操作系统告诉你内存不足,此时调用与低内存相关的委托方法:

- (void)didReceiveMemoryWarning

为了让运行的应用程序有机会清除一些残骸,以便不必终止任何事情,如果没有足够的空间,应用程序就会开始被杀死。除非你能看到仪器泄漏,否则我真的不认为这是内存泄漏。