iOS 4.3上的图像负面失真

时间:2012-04-09 17:00:41

标签: iphone ios image ipad

我想,当我在iPad模拟器和设备上使用iOS 4.3时,为什么我的应用程序会出现图像失真。看截图。在iOS 5.1模拟器和设备上正常显示相同的图像。看看第二个截图......我该如何管理?可能在显示JPEG之前编写一些代码? Distorted on iOS 4.3 Normal on iOS 5.1 不幸的是,我必须修复它,因为我的应用程序必须以某种方式在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];

是的,我忘了提及,这些人只是视图控制器的自定义按钮。

1 个答案:

答案 0 :(得分:1)

我在4.3上遇到了同样的问题 - 结果发现图像是用CMYK颜色保存的。切换到RGB并重新保存图像会使问题消失。