gpt4 book ai didi

ios - 从 UIImageView 创建 UIImage

转载 作者:行者123 更新时间:2023-12-01 17:31:23 25 4
gpt4 key购买 nike

我创建了一个 UIImageView有两个 UILabel作为它的 subview :

 UIImageView *img=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, [[UIScreen mainScreen] bounds].size.height)];
img.backgroundColor=self.view.backgroundColor;

UILabel *textOne=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 290, 250)];
textOne.text=_textOneLabel.text;
textOne.textColor=_textOneLabel.textColor;

UILabel *textTwo = [[UILabel alloc] initWithFrame:CGRectMake(100,180,200,100)];
textTwo.text=_textTwoLabel.text;
textTwo.textColor=_textTwoLabel.textColor;

[img addSubview:textOne];
[img addSubview:textTwo];

UIImageWriteToSavedPhotosAlbum(img.image, nil, nil, nil); //img.image is null

我想创建一个 UIImageUIImageView 将其保存在相机胶卷中和 2 创建 UILabel . img.image返回 null 因为没有分配给 UIImageView 的图像.
您知道如何实现这一目标吗?

最佳答案

使用以下方法将您的 UIImageView 转换为 UIImage。

UIImage *image = [self ChangeImageViewToImage:img];

//方法
-(UIImage *) ChangeImageViewToImage : (UIImageView *) view{

UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage * img = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return img;

}

关于ios - 从 UIImageView 创建 UIImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20719518/

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