Ios 将完整UIImageView渲染为位图图像

Ios 将完整UIImageView渲染为位图图像,ios,uiimageview,uiimage,Ios,Uiimageview,Uiimage,我知道如何将普通UIView渲染为位图图像: UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, 0.0); [view.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *bitmapImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); 问题

我知道如何将普通UIView渲染为位图图像:

UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, 0.0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *bitmapImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

问题是,如果
视图
是一个
UIImageView
,并且从
ResizeableImageWithCapinsets:
返回了一个可拉伸的图像,则以这种方式获得的
位图图像
是原始图像-未拉伸的图像-而不是真正显示的拉伸图像。通过
bitmapImage
view
之间的大小差异,我可以很容易地看出这一点。因此,我的问题是如何呈现图像为可拉伸大小图像的
UIImageView
所显示的全部内容?

因此,我尝试了以下代码:

UIEdgeInsets edgeInsets = UIEdgeInsetsMake(-20, -20, -20, -20);
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)];
imageView.image = [[UIImage imageNamed:@"number1.png"] resizableImageWithCapInsets:edgeInsets];
[self.view addSubview:imageView];
UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, NO, 0.0);
[imageView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *bmImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

NSArray *documentsPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentsPaths objectAtIndex:0];
[UIImagePNGRepresentation(bmImage) writeToFile:[documentsDir stringByAppendingString:@"num1.png"] atomically:YES];
得到这个:

由此:


因此,代码似乎按预期工作。

我是个白痴!代码工作正常。我只是在别的地方把事情搞砸了。愚蠢的是,我将代码放在UIView类别的方法中,但我将该方法命名为
image
。它非常适用于一般的
UIView
s,但由于
UIImageView
有自己的
image
方法,
UIImageView
UIView
的一个子类,
UIImageView
image
方法在我试图获取位图图像时被调用。

你能澄清这个问题吗?当然。你不清楚哪一部分?