我有一个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; }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。