目标:缩放后如何裁剪图像。我可以在(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)