[英]Image crop not properly working in iOS 6.0. Working fine in simulator.
- (UIImage *)imageByCropping:(UIImage *)imageToCrop toRect:(CGRect)rect
{
CGImageRef imageRef = CGImageCreateWithImageInRect([imageToCrop CGImage], rect);
UIImage *cropped = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return cropped;
}
我正在使用此代码。 请给出一些解决方案。谢谢
CGImageCreateWithImageInRect
无法正确处理图像方向。 网上有许多奇怪而精彩的裁剪技术,涉及巨型开关/案例陈述(参见Ayaz答案中的链接),但是如果你留在UIKit级并且只使用UIImage
本身的方法来绘图,那么详细的细节是照顾你的。
以下方法尽可能简单,并且适用于我遇到的所有情况:
- (UIImage *)imageByCropping:(UIImage *)image toRect:(CGRect)rect
{
if (UIGraphicsBeginImageContextWithOptions) {
UIGraphicsBeginImageContextWithOptions(rect.size,
/* opaque */ NO,
/* scaling factor */ 0.0);
} else {
UIGraphicsBeginImageContext(rect.size);
}
// stick to methods on UIImage so that orientation etc. are automatically
// dealt with for us
[image drawAtPoint:CGPointMake(-rect.origin.x, -rect.origin.y)];
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return result;
}
如果不需要透明度,可能需要更改opaque
参数的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.