2013-04-21 132 views
1

我正在开发一个应用程序来裁剪图像。 我使用下面的代码来调用裁剪的方法:裁剪图像让图片左旋

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,我发现返回的图像向左旋转。我无法将它旋转起来。 任何人都可以知道这个问题?

回答

1

由于大卫写的问题很可能是你不保存imageOrientation属性。你的代码的问题还在于你不保存比例尺属性。 尝试这样创建裁剪图像:

CGImageRef croppedRef = CGBitmapContextCreateImage(currentContext); 
UIImage* cropped = [UIImage imageWithCGImage:croppedRef scale:imageToCrop.scale orientation:imageToCrop.imageOrientation]; 
CGImageRelease(croppedRef); 
+0

嗨,我这样做的时候,我的旋转仍然关闭。你能想到使用imageOrientation不能正常工作的原因吗? – Aggressor 2014-09-22 21:43:51

2

Uiimage有一个imageOrientation在某些情况下指示系统应显示图像位的方式的属性。尝试记录该值并查看。如果它是我怀疑的那么你可能需要调整你的裁剪算法,首先创建一个CGImage,然后使用UIImage方法从它制作一个具有方向参数的UIImage。