gpt4 book ai didi

ios - 将完整的 UIImageView 渲染为位图图像

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:54:04 25 4
gpt4 key购买 nike

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

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

问题是,如果 view 是一个 UIImageView,带有从 resizableImageWithCapInsets: 返回的拉伸(stretch)可调整大小的图像,bitmapImage 这样得到的是原始图像——未拉伸(stretch)的图像——而不是真正显示的拉伸(stretch)图像。我可以通过 bitmapImageview 之间的大小差异轻松判断。所以我的问题是如何呈现 UIImageView 显示的全部内容,其图像是拉伸(stretch)的可调整大小的图像?

最佳答案

所以,我尝试了这段代码:

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];

得到这个:

enter image description here

从这里开始:

enter image description here

所以看起来代码可以正常工作。

关于ios - 将完整的 UIImageView 渲染为位图图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8788376/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com