UIView to Image

效果:


92DD32C0C376FC056BBFCAEE14FF639B.jpg

主要代码:

@interface ShopViewController ()

@property(nonatomic,strong)UIView *yView;

@end

@implementation ShopViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [self initView];

    [self createBtn];

}

-(void)createBtn
{
    UIButton *transferBtn=[[UIButton alloc]initWithFrame:CGRectMake(100,200,100,30)];
    [transferBtn setTitle:@"保存图片" forState:0];
    [transferBtn setTitleColor:[UIColor redColor] forState:0];
    [transferBtn addTarget:self action:@selector(transferBtnEvent) forControlEvents:UIControlEventTouchUpInside ];
    [self.view addSubview:transferBtn];
}

-(void)transferBtnEvent
{
    UIImage *image=[self createViewImage:self.yView];
    UIImageWriteToSavedPhotosAlbum(image,self,@selector(image:didFinishSavingWithError:contextInfo:),nil);
}

-(void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    if (error==nil)
    {
        UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"提示" message:@"图片保存成功" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alertView show];

        NSLog(@"保存成功");
    }
    else
    {
        UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"提示" message:@"图片保存失败" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alertView show];

        NSLog(@"保存失败呢");
    }
}

-(UIImage *)createViewImage:(UIView *)yView
{
    UIGraphicsBeginImageContextWithOptions(yView.bounds.size,NO,[UIScreen mainScreen].scale);
    [yView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image=UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

-(void)initView
{
    self.yView=[[UIView alloc]initWithFrame:CGRectMake(0,0,APPW,APPH-kmargin*2)];
    self.yView.backgroundColor=[UIColor lightGrayColor];

    // label
    UILabel *tipLabel=[[UILabel alloc]initWithFrame:CGRectMake(10,30,APPW-20,30)];
    tipLabel.text=@"UIViewToUIImage";
    tipLabel.textAlignment=NSTextAlignmentCenter;
    tipLabel.textColor=[UIColor redColor];
    [self.yView addSubview:tipLabel];

    // image
    UIImageView *image=[[UIImageView alloc]initWithFrame:CGRectMake(kmargin*2,kmargin*4+tipLabel.frame.size.height,APPW-kmargin*4,APPH-kmargin*12)];
    image.image=[UIImage imageNamed:@"2.jpg"];
    [self.yView addSubview:image];
}
@end
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,581评论 25 708
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,257评论 4 61
  • 感恩客户如潮水般的涌来,为公司带来收入的增长,谢谢! 感恩融洽的同事关系,谢谢! 感恩婆婆在暑假期间帮我照顾孩子,...
    深山_132f阅读 158评论 0 5
  • 最近被轰轰烈烈的考试周虐成狗,作为一个英语转经济的文科狗,我首先要迈的就是数学这道坎:辛辛苦苦学了一学期的高数,觉...
    流波舞阅读 3,100评论 1 9
  • 今夜世界是两个人 一个生而为王 一个死而后已 今夜,你会思念谁? 今天有一种说不出的滋味在心里...
    墨尘望雪阅读 569评论 0 3