从 UIImageView 创建 UIImage

Create UIImage from a UIImageView(从 UIImageView 创建 UIImage)

本文介绍了从 UIImageView 创建 UIImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个 UIImageView,其中有两个 UILabel 作为它的子视图:

I create a UIImageView with two UILabel as subviews of it:

 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

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

I want to create a UIImage to save it in the camera roll with the UIImageView and the 2 created UILabel. img.image returns null because there is no image assigned to the UIImageView.
Do you have an idea of how achieving that?

推荐答案

使用下面的方法将你的 UIImageView 转换为 UIImage.

Use below method to convert your UIImageView to 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;

}

这篇关于从 UIImageView 创建 UIImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:从 UIImageView 创建 UIImage

基础教程推荐