Extract a part of UIImageView(提取部分 UIImageView)
问题描述
我想知道是否可以提取" UIImageView
的一部分.
I was wondering if it's possible to "extract" a part of UIImageView
.
例如,我使用 Warp Affine 选择 UIImageView
的一部分,并且我知道所选部分 frame
.
For example, I select using Warp Affine a part of the UIImageView
and I know the selected part frame
.
喜欢这张图片:
是否可以从原始UIImageView
中只获取选定的部分而不损失质量?
Is it possible to get from the original UIImageView
only the selected part without losing quality?
推荐答案
通过category方法获取视图的快照:
Get the snapshot of the view via category method:
@implementation UIView(Snapshot)
-(UIImage*)makeSnapshot
{
CGRect wholeRect = self.bounds;
UIGraphicsBeginImageContextWithOptions(wholeRect.size, YES, [UIScreen mainScreen].scale);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[[UIColor blackColor] set];
CGContextFillRect(ctx, wholeRect);
[self.layer renderInContext:ctx];
UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
@end
然后通过另一种类别方法将其裁剪到您的矩形:
then crop it to your rect via another category method:
@implementation UIImage(Crop)
-(UIImage*)cropFromRect:(CGRect)fromRect
{
fromRect = CGRectMake(fromRect.origin.x * self.scale,
fromRect.origin.y * self.scale,
fromRect.size.width * self.scale,
fromRect.size.height * self.scale);
CGImageRef imageRef = CGImageCreateWithImageInRect(self.CGImage, fromRect);
UIImage* crop = [UIImage imageWithCGImage:imageRef scale:self.scale orientation:self.imageOrientation];
CGImageRelease(imageRef);
return crop;
}
@end
在你的 VC 中:
UIImage* snapshot = [self.imageView makeSnapshot];
UIImage* imageYouNeed = [snapshot cropFromRect:selectedRect];
selectedRect
应该在你的 self.imageView
坐标系中,如果没有那么使用selectedRect = [self.imageView convertRect:selectedRect fromView:...]
selectedRect
should be in you self.imageView
coordinate system, if no so then use
selectedRect = [self.imageView convertRect:selectedRect fromView:...]
这篇关于提取部分 UIImageView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:提取部分 UIImageView
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01