做了一個剪裁圖片的程序,調用剪裁:
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);