如何使CGImage分辨率依赖?

时间:2013-08-07 10:49:59

标签: ios uiimageview uiimage cgimage

我想裁剪图片,所以,我谷歌,并找到这篇文章:

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

但问题出在这一行:

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

我意识到如果图像在非视网膜显示器上,它会完美切割,但如果在视网膜设备上,则矩形框架的宽度和高度需要多达2.但问题是,我该如何制作它更通用吗? (我不想检测设备)谢谢。

1 个答案:

答案 0 :(得分: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;
}

希望这有帮助。