我有一个由多个UIImageView组成的自定义视图。这些UIImageView可以在大小上有所不同(从200x200到50x50)。它们显示的图像(从png文件加载)以尽可能高的分辨率(200x200)存储在资源中,但通常以较低的分辨率显示。使用iPhone上的UIImageView缩放图像
目前我只加载与图像的UIImageViews在全分辨率,这样的:
UIImage* anImage = [UIImage imageWithContentsOfFile:imagePathAndName]; // anImage is 200x200
aView = [[UIImageView alloc] initWithImage:anImage];
aView.frame = myFrame; // myFrame is 50x50
第一个问题,有没有性能损失(内存或CPU时间),只是让UIImageView的距离缩小图像发言权200x200到50x50?看起来我的应用程序并没有使用大量的内存,或者由于这个原因而减慢了速度,但是如果我可以加快速度或减少内存消耗,那将是一个胜利。
我玩过程序化缩放图像。就像这样:
UIImage* anImage = [UIImage imageWithContentsOfFile:imagePathAndName]; // Image is UIGraphicsBeginImageContext(scaledSize); // scaledSize is 50x50
CGRect scaledRect = CGRectMake(0.0, 0.0, scaledSize.width, scaledSize.height);
[anImage drawInRect:scaledRect];
UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext(); // Scaled image is now 50x50
UIGraphicsEndImageContext();
aView = [[UIImageView alloc] initWithImage:scaledImage];
aView.frame = myFrame; // myFrame is 50x50
编程缩放图像第一似乎没有任何不良影响......直到我装在我的iPhone 4看,的UIImageView的大小是根据大小,如果它的父视图, (放在IB中)是iPhone 3G屏幕的大小。不幸的是,尽管iPhone 4的分辨率是3G的两倍,但在iPhone 4上运行时,这个视图的大小是一样的。因此,通过根据视图大小缩放图像,我最终使它们的分辨率比iPhone 4上的要低,而且看起来有些蹩脚。
所以第二个问题是,如果以编程方式缩小图像有一定的好处。我怎样才能利用iPhone 4上的额外像素?显然,我不能按照我的观点来看。
UIGraphicsBeginImageContextWithOptions的伎俩完美,谢谢!你对过早优化的观点很好,我不得不考虑一下。 – zpasternack 2010-08-13 17:53:02