Iphone 提取UIImageView的一部分

Iphone 提取UIImageView的一部分,iphone,objective-c,uiimageview,resize,warp,Iphone,Objective C,Uiimageview,Resize,Warp,我想知道是否可以“提取”UIImageView的一部分 例如,我使用扭曲仿射选择UIImageView的一部分,并且我知道所选的部分frame 如图所示: 是否可以从原始的UIImageView仅获取所选零件而不丢失质量?是的,这是可能的。首先,您应该使用此属性获取UIImageView的图像: @property(nonatomic, retain) UIImage *image; 和NSImage的: @property(nonatomic, readonly) CGImageRef C

我想知道是否可以“提取”UIImageView的一部分

例如,我使用扭曲仿射选择
UIImageView
的一部分,并且我知道所选的部分
frame

如图所示:


是否可以从原始的
UIImageView
仅获取所选零件而不丢失质量?

是的,这是可能的。首先,您应该使用此属性获取UIImageView的图像:

@property(nonatomic, retain) UIImage *image;
和NSImage的:

@property(nonatomic, readonly) CGImageRef CGImage;
然后,您将获得剪切图像:

CGImageRef cutImage = CGImageCreateWithImageInRect(yourCGImageRef, CGRectMake(x, y, w, h));
如果再次需要UIImage,应使用此UIImage的方法:

+ (UIImage *)imageWithCGImage:(CGImageRef)cgImage;

PS:我不知道如何直接操作,如果不将其转换为CGImageRef,也许有一种方法。

是的,这是可能的。首先,您应该使用以下属性获取UIImageView的图像:

@property(nonatomic, retain) UIImage *image;
和NSImage的:

@property(nonatomic, readonly) CGImageRef CGImage;
然后,您将获得剪切图像:

CGImageRef cutImage = CGImageCreateWithImageInRect(yourCGImageRef, CGRectMake(x, y, w, h));
如果再次需要UIImage,应使用此UIImage的方法:

+ (UIImage *)imageWithCGImage:(CGImageRef)cgImage;

PS:我不知道如何直接操作,如果不将其转换为CGImageRef,可能有一种方法。

通过分类方法获取视图的快照:

@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
@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
然后通过另一种分类方法将其裁剪到您的rect:

@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
@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:…]

通过分类方法获取视图的快照:

@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
@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
然后通过另一种分类方法将其裁剪到您的rect:

@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
@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:…]

显示您的尝试,x、y、w、h只是符号,您使用的坐标和视图坐标是什么?UIImageView坐标是:20,8280400。UIImageView坐标的选定部分是:31255.5262142。如果超出边界,原点是(20,8),其他点是宽度/高度。因此,例如,如果您想要图像的下半部分,请写入x=220,y=300,w=60,h=100。如果这不是精确的坐标,尝试稍微更改rect坐标和大小。顺便说一句,我不确定UIImage是否相同。显示您尝试过的,x、y、w、h只是符号,您使用的坐标和视图坐标是什么?UIImageView坐标是:20,8280400。UIImageView坐标的选定部分是:31255.5262142。如果超出边界,原点是(20,8),其他点是宽度/高度。因此,例如,如果您想要图像的下半部分,请写入x=220,y=300,w=60,h=100。如果这不是精确的坐标,尝试稍微更改rect坐标和大小。顺便说一句,我不确定UIImage是否相同。很抱歉,您的图像裁剪方法效果不好。它似乎返回图像的随机部分,而不是所选部分。我目前正在使用这种方法:它似乎工作得很好,但有点不准确,也许你可以帮我修复它?例如,如果我拍摄iPhone屏幕,并将其全部选中,它将返回所有选中的iPhone屏幕,而不带状态栏。哦,后者不是UIImageView的方法,而是UIImage category方法。首先,你需要获取UIImageView的快照,然后用后一种方法裁剪快照。伙计,我再次尝试了你所说的方法,现在它工作得很好,就像我发布的一样。但它仍然是不准确的,它不是100%完美的,就像我的一样。我可以制作一段视频给你看吗?非常感谢。请参阅编辑。您选择的矩形是在什么坐标系下?很抱歉,您的图像裁剪方法效果不好。它似乎返回图像的随机部分,而不是所选部分。我目前正在使用这种方法:它似乎工作得很好,但有点不准确,也许你可以帮我修复它?例如,如果我拍摄iPhone屏幕,并将其全部选中,它将返回所有选中的iPhone屏幕,而不带状态栏。哦,后者不是UIImageView的方法,而是UIImage category方法。首先,你需要获取UIImageView的快照,然后用后一种方法裁剪快照。伙计,我再次尝试了你所说的方法,现在它工作得很好,就像我发布的一样。但它仍然是不准确的,它不是100%完美的,就像我的一样。我可以制作一段视频给你看吗?非常感谢。请参阅编辑。您选择的矩形在什么坐标系下?您是如何选择此区域的?=)您是如何选择此区域的?=)