我想,为什么当我在 iPad 模拟器和设备上使用 iOS 4.3 时,我的应用程序会出现图像失真。看一下截图。相同的图像在 iOS 5.1 模拟器和设备上正常显示。看第二个屏幕截图...我应该如何处理这个问题?也许可以在显示 JPEG 之前编写一些代码? 不幸的是,我必须修复它,因为我的应用程序必须以某种方式在 iOS 4.3 上运行。
这是一个方法,用于生成拇指:
- (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize
{
UIImage *sourceImage = self;
UIImage *newImage = nil;
CGSize imageSize = sourceImage.size;
CGFloat width = imageSize.width;
CGFloat height = imageSize.height;
CGFloat targetWidth = targetSize.width;
CGFloat targetHeight = targetSize.height;
CGFloat scaleFactor = 0.0;
CGFloat scaledWidth = targetWidth;
CGFloat scaledHeight = targetHeight;
CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
if (CGSizeEqualToSize(imageSize, targetSize) == NO)
{
CGFloat widthFactor = targetWidth / width;
CGFloat heightFactor = targetHeight / height;
if (widthFactor > heightFactor)
scaleFactor = widthFactor; // scale to fit height
else
scaleFactor = heightFactor; // scale to fit width
scaledWidth = width * scaleFactor;
scaledHeight = height * scaleFactor;
// center the image
if (widthFactor > heightFactor)
{
thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
}
else
if (widthFactor < heightFactor)
{
thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
}
}
UIGraphicsBeginImageContext(targetSize); // this will crop
CGRect thumbnailRect = CGRectZero;
thumbnailRect.origin = thumbnailPoint;
thumbnailRect.size.width = scaledWidth;
thumbnailRect.size.height = scaledHeight;
[sourceImage drawInRect:thumbnailRect];
newImage = UIGraphicsGetImageFromCurrentImageContext();
if(newImage == nil)
NSLog(@"could not scale image");
//pop the context to get back to the default
UIGraphicsEndImageContext();
return newImage;
}
但是,如果我不使用它来准备图像,它仍然是负数,但运行速度会更慢,因为图像会自动调整大小。
该方法用于显示图像:
-(void) setImage:(UIImage*)image forState:(UIControlState) state
{
[self.buttonView setImage:image forState:state];
}
这样:
[current setImage:thumb forState:UIControlStateNormal];
是的,我忘了提及,这些家伙只是 View Controller 的自定义按钮。
最佳答案
我在 4.3 上遇到了同样的问题 - 原来图像是用 CMYK 颜色保存的。切换到 RGB 并重新保存图像使问题消失。
关于iphone - iOS 4.3 上的图像负畸变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10076790/