问题描述
- 剪裁图片导致图片向左旋转
- 做了一个剪裁图片的程序,调用剪裁:
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的时候,图片向左旋转了。而且我也没办法让他向上旋转,怎么办?
解决方案
如楼上所说的可能是imageOrientation属性的问题。并且你也没有正确保存scale。这样创建剪裁的图片:
CGImageRef croppedRef = CGBitmapContextCreateImage(currentContext);UIImage* cropped = [UIImage imageWithCGImage:croppedRef scale:imageToCrop.scale orientation:imageToCrop.imageOrientation];CGImageRelease(croppedRef);
解决方案二:
UIImage有一个imageOrientation 属性,在某些情况下指示系统应该显示图片的方法,记录一下这个值。如果我猜的正确,应该调整剪裁算法,先创建一个CGImage,然后使用UIImage方法来进行方向参数的相关实现。
时间: 2024-09-24 13:29:08