目标:如何在缩放后裁剪图像。我可以在缩放预定义坐标后裁剪图像,如(10,10,320,312)。我的UIImageView有这个尺寸吗?
我在UIImageView
中有一个UIScrollView
,我可以缩放图片。当我按cropButton
时,我希望在缩放后,UIImage
中显示的图像被裁剪。我希望图片大小等于UIImageView
大小。
谢谢!!
缩放前缩放并裁剪
后我的代码在这里。
-(void)cropButtonClicked
{
//Calculate the required area from the scrollview
CGRect visibleRect;
float scale = 1.0f/scroll.zoomScale;
visibleRect.origin.x = scroll.contentOffset.x * scale;
visibleRect.origin.y = scroll.contentOffset.y * scale;
visibleRect.size.width = scroll.bounds.size.width * scale;
visibleRect.size.height = scroll.bounds.size.height * scale;
UIImage *image = [self imageByCropping:imageView.image toRect:visibleRect];
imageView.image=image;
}
- (UIImage*)imageByCropping:(UIImage *)myImage toRect:(CGRect)cropToArea{
CGImageRef cropImageRef = CGImageCreateWithImageInRect(myImage.CGImage, cropToArea);
UIImage* cropped = [UIImage imageWithCGImage:cropImageRef];
CGImageRelease(cropImageRef);
return cropped;
}
I use this logic.. Am I RIGHT? (May be i have to check my ScrollView programming)
答案 0 :(得分:1)
愿这段代码全部为您使用.. !!
CGSize itemSize = CGSizeMake(newWidth, newHeight);<br>
UIGraphicsBeginImageContextWithOptions(itemSize, NO, 0);<br>
CGRect imageRect = CGRectMake(0, 0, newWidth, newHeight);<br>
[image drawInRect:imageRect];<br>
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();<br>
UIGraphicsEndImageContext();