ios – 为什么我的UIImage占用了这么多内存?

我有一个UI Image,我正在加载到我的应用程序的一个视图中.它是一个10.7 MB的图像,但当它在应用程序中加载时,应用程序的资源使用量突然增加50 MB.为什么这样做?不应该使用的内存仅增加约10.7MB?我确信加载图像是导致内存使用量跳跃的原因,因为我尝试将这些行注释掉,内存使用量回到8 MB左右.这是我加载图像的方式:
UIImage *image = [UIImage imageNamed:@"background.jpg"];
self.backgroundImageView = [[UIImageView alloc] initWithImage:image];
[self.view addSubview:self.backgroundImageView];

如果没有办法减少这个图像使用的内存,有没有办法强制它在我想要它时解除分配?我正在使用ARC.

解决方法

正如@rckoenes所说
不要显示文件大小的图像.
您需要在显示图像之前调整图像大小.
UIImage *image = [UIImage imageNamed:@"background.jpg"];
self.backgroundImageView =[self imageWithImage:display scaledToSize:CGSizeMake(20,20)];//Give your CGSize of the UIImageView.
[self.view addSubview:self.backgroundImageView];


-(UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
    //UIGraphicsBeginImageContext(newSize);
    // In next line,pass 0.0 to use the current device's pixel scaling factor (and thus account for Retina resolution).
    // Pass 1.0 to force exact pixel size.
    UIGraphicsBeginImageContextWithOptions(newSize,NO,0.0);
    [image drawInRect:CGRectMake(0,newSize.width,newSize.height)];
    UIImage *newImage = UIGraphicsGetimageFromCurrentimageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...