我可以通过(10,10,320,312)等坐标裁剪图像吗? [英] can i crop image by coordinates like (10,10,320,312)?
本文介绍了我可以通过(10,10,320,312)等坐标裁剪图像吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
目标:缩放后如何裁剪图像。在以(10,10,320,312)等预定义坐标缩放后,我可以裁剪图像吗?我的UIImageView有这个大小吗?
Goal : How Crop a image after zooming. can i crop image after zooming at predefined coordinates like (10,10,320,312). my UIImageView has this size?
我在 UIScrollView $中有一个
UIImageView
c $ c>,然后可以缩放图像。我想要在缩放后按 cropButton
后在 UIImage
中显示的图像被裁剪。我希望图像的大小等于 UIImageView
大小。
I have a UIImageView
in a UIScrollView
and I can zoom my image. I wants after zooming when I press cropButton
the image which is displaying in UIImage
is cropped. I wants size of image equal to UIImageView
size.
感谢!!
缩放前
缩放并裁剪后
before zooming
after zoom and crop
我的代码在这里。
-(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)
推荐答案
愿这段代码对您有用。
May this code use full for you..!!
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();
这篇关于我可以通过(10,10,320,312)等坐标裁剪图像吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文