【问题标题】:UIImage loop with image scale - Received memory warning具有图像比例的 UIImage 循环 - 收到内存警告
【发布时间】:2013-10-29 07:40:58
【问题描述】:

我有内存泄漏问题,自 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

【问题讨论】:

  • 通过 Leaks 仪器运行它时发现了什么?
  • 为什么要将图像数据保存在数组中???如果您将图像保存在 Array 中,那么您的 RAM 肯定会被填满并发出内存警告。
  • 您好,您有其他选择吗?我将图像保存在一个数组中,因为我的下一个方法将这些图像合并到一个视频文件中。

标签: objective-c memory-leaks uiimage


【解决方案1】:

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

- (void)didReceiveMemoryWarning

如果没有足够的空间让应用程序开始被杀死,那么为了让正在运行的应用程序有机会清除一些杂物以便不必终止任何东西。除非您可以在仪器中看到泄漏,否则我真的不认为这是内存泄漏。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-31
    • 1970-01-01
    • 2011-04-30
    • 1970-01-01
    • 2012-06-10
    • 1970-01-01
    • 1970-01-01
    • 2013-03-12
    相关资源
    最近更新 更多