我有一个要加载到我的应用程序视图中的UIImage。它是一个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, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句