gpt4 book ai didi

ios - 捕获 UIView 并另存为图像

转载 作者:IT王子 更新时间:2023-10-29 08:10:18 26 4
gpt4 key购买 nike

首先,我在 UIImageView 上添加了 UILable,然后在截取 UIView 后,图像无法正确捕获 UIView Frame 我还附上了图片 url。

enter image description here

1) Original Image Link:

enter image description here

2) After Capture the image Link:

代码:

UIGraphicsBeginImageContext(viewImage.frame.size);
[viewImage.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *vwImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

NSData *data=UIImagePNGRepresentation(vwImage);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
// NSString *imgName = [NSString stringWithFormat:imagename];
NSString *strPath = [documentsDirectory stringByAppendingPathComponent:imagename];
[data writeToFile:strPath atomically:YES];

最佳答案

您可以使用以下代码截取任何 UIView 的屏幕截图或捕获任何 UIView 并将其保存为图像。

- (UIImage *)captureView {

//hide controls if needed
CGRect rect = [self.view bounds];

UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[self.view.layer renderInContext:context];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;

}

关于ios - 捕获 UIView 并另存为图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17145049/

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