0

我想裁剪一个图像,所以,我谷歌,找到这篇文章:

http://iosdevelopertips.com/graphics/how-to-crop-an-image.html

但问题出在这一行:

CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], rect);

我意识到如果图像在非视网膜显示器上,它会完美切割,但如果在视网膜设备上,矩形框架的宽度和高度需要倍数 2。但问题是,我怎样才能让它更通用? (我不想检测设备)谢谢。

4

1 回答 1

0

已经用以下代码克服了同样的问题:

-(UIImage *)imageFromImage:(UIImage *)image inRect:(CGRect)rect {
rect.size.height = rect.size.height * [image scale];
rect.size.width = rect.size.width * [image scale];
rect.origin.x = rect.origin.x * [image scale];
rect.origin.y = rect.origin.y * [image scale];
CGImageRef sourceImageRef = [image CGImage];
CGImageRef newImageRef = CGImageCreateWithImageInRect(sourceImageRef, rect);
UIImage *newImage = [UIImage imageWithCGImage:newImageRef scale:[image scale] orientation:[image imageOrientation]];
CGImageRelease(newImageRef);
return newImage;
}

希望这可以帮助。

于 2013-08-07T11:37:05.787 回答