2 yue1liang1chuan yue1liang1chuan 于 2013.04.22 11:05 提问

剪裁图片导致图片向左旋转

做了一个剪裁图片的程序,调用剪裁:

UIImage* croppedImage = [self imageCrop:imageView toRect:CGRectMake(10.0, 50.0, 320, 100)];

方法:

{
    //create a context to do our clipping in
    CGRect newRect = CGRectApplyAffineTransform(rect, imageViewToCrop.transform);
    UIImage *imageToCrop = imageViewToCrop.image;
    UIGraphicsBeginImageContext(newRect.size);
    CGContextRef currentContext = UIGraphicsGetCurrentContext();
    //create a rect with the size we want to crop the image to
    //the X and Y here are zero so we start at the beginning of our
    //newly created context
    CGRect clippedRect = CGRectMake(0, 0, rect.size.width, rect.size.height);
    CGContextClipToRect( currentContext, clippedRect);
    //draw the image to our clipped context using our offset rect
    CGContextDrawImage(currentContext, newRect, imageToCrop.CGImage);
    //pull the image from our cropped context
    UIImage *cropped = UIGraphicsGetImageFromCurrentImageContext();
    //pop the context to get back to the default
    UIGraphicsEndImageContext();
    return cropped;
}

设置图片到UIImageview的时候,图片向左旋转了。而且我也没办法让他向上旋转,怎么办?

2个回答

lianlianbushell
lianlianbushell   2013.04.22 14:44
已采纳

如楼上所说的可能是imageOrientation属性的问题。并且你也没有正确保存scale。这样创建剪裁的图片:

CGImageRef croppedRef = CGBitmapContextCreateImage(currentContext);
UIImage* cropped = [UIImage imageWithCGImage:croppedRef scale:imageToCrop.scale orientation:imageToCrop.imageOrientation];
CGImageRelease(croppedRef);
lanhailvcao
lanhailvcao   2013.04.22 14:02

UIImage有一个imageOrientation 属性,在某些情况下指示系统应该显示图片的方法,记录一下这个值。如果我猜的正确,应该调整剪裁算法,先创建一个CGImage,然后使用UIImage方法来进行方向参数的相关实现。

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!