在iPhone上加载大图的最快方式是什么?
问题描述:
我正在建设一个滚动视图,通过100个房屋的图像轻扫。 它的工作原理。但是....对于每个查看的图像,分配的内存增加2.5 MB。最终,应用程序崩溃,因为它的内存不足。我使用代码解压图像..... - (void)解压缩const CGImageRef cgImage = [self CGImage];
const int width = CGImageGetWidth(cgImage);
const int height = CGImageGetHeight(cgImage);
const CGColorSpaceRef colorspace = CGImageGetColorSpace(cgImage);
const CGContextRef context = CGBitmapContextCreate(
NULL, /* Where to store the data. NULL = don’t care */
width, height, /* width & height */
8, width * 4, /* bits per component, bytes per row */
colorspace, kCGImageAlphaNoneSkipFirst);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), cgImage);
CGContextRelease(context);
}
,但其没有工作,非常及时采取加载图像。
答
我也面临同样的问题我选择的解决方案是我在320 * 460尺寸调整图像的大小,然后图像大小变成大约50 kb不超过。你也可以做同样的事情。
代码调整图像: -
假设您在UIImage的新图像的图像变量存储在editedImage这也是UIImage对象图像。
if (self.image.size.height>460 || self.image.size.width>320) {
UIGraphicsBeginImageContext(CGSizeMake(320,480));
[self.image drawInRect: CGRectMake(0, 0,320,480)];
self.editedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSLog(@"smallImage%f",self.editedImage.size.height);
NSLog(@"smallImage%f",self.editedImage.size.width);
}
else {
self.editedImage=self.image;
}
答
你需要做一些内存优化。这是一个节省内存的逻辑,但你需要实现它自己。不要将所有图像添加到滚动视图,只需添加初始5张图像。假设用户正在查看image1,现在他在用户到达图像时到达image3 3从scrollView中删除image1,以便释放内存。并且随着用户向前滚动添加下一张图像,例如当用户在image4上时添加image6,反之亦然。当用户从image4转到image3时,从内存中删除image6。这样,只有5张图像在内存中。
答
方式三:
1: Trying Apple's sample code called "StreetScroller", then you'll get to know how to make scrollview work definitely.
2:Create thumbnails for large images and save to your own directory. Then read them from url.
3: Use UIpageviewcontroller
什么是图像的尺寸? – Andrew 2011-06-02 07:21:52
你为什么不懒惰地加载它? – 2011-06-02 07:27:54
尺寸300 * 404分辨率162.99 – georgina 2011-06-02 07:34:02