合体冰冰及文字水印

#import "ViewController.h"


@interface ViewController ()

{

    UIImageView *_imageView;

}

@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    

    __block UIImage *image1;

    __block UIImage *image2;

    NSOperationQueue *queue = [[NSOperationQueue alloc] init];

    NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{

        NSLog(@"----下载图片1-----%@", [NSThread currentThread]);

        NSURL *url1 = [NSURL URLWithString:@"http://a.hiphotos.baidu.com/image/pic/item/8718367adab44aed3eb376e9b11c8701a18bfb3a.jpg"];

        NSData *data1 = [NSData dataWithContentsOfURL:url1];

        //NSLog(@"data1 %@",data1);

        image1 = [UIImage imageWithData:data1];

    }];

    [operation addExecutionBlock:^{

        NSLog(@"-----下载图片2----%@",[NSThread currentThread]);

        NSURL *url2 = [NSURL URLWithString:@"https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superplus/img/logo_white_ee663702.png"];

        NSData *data2 = [NSData dataWithContentsOfURL:url2];

        //NSLog(@"data2 %@", data2);

        image2 = [UIImage imageWithData:data2];

    }];

    [operation setCompletionBlock:^{

        NSLog(@"-----合并图片------%@",[NSThread currentThread]);

        

        if (image1 && image2) {

            //开启一个位图上下文

            UIGraphicsBeginImageContext(image1.size);

            

            //绘制图片1

            [image1 drawInRect:CGRectMake(0, 0, image1.size.width, image1.size.height)];

            //绘制图片2

            [image2 drawInRect:CGRectMake(image1.size.width-image2.size.width*0.4, image1.size.height-image2.size.height*0.4, image2.size.width*0.4, image2.size.height*0.4)];

            

            NSString *name=@"范冰冰";

            //属性字典

            NSDictionary *attributeDic=[NSDictionary dictionaryWithObjectsAndKeys:[UIColor redColor],NSForegroundColorAttributeName,[UIFont boldSystemFontOfSize:50],NSFontAttributeName, nil];

            //NSString

            [name drawAtPoint:CGPointMake(0, image1.size.height-50-5) withAttributes:attributeDic];

            

            //label

            UILabel *description=[[UILabel alloc] initWithFrame:CGRectMake(100, 100, 300, 50)];

            description.textColor=[UIColor purpleColor];

            [description setFont:[UIFont systemFontOfSize:50]];

            [description setText:@"还珠格格"];

            //获取label图片

            UIImage *labelImage=[self getImageFromLabel:description];

            [labelImage drawInRect:CGRectMake(0, 80, labelImage.size.width, labelImage.size.height)];

            

            //得到上下文中的图片

            UIImage *fullImage = UIGraphicsGetImageFromCurrentImageContext();

            

            //结束上下文

            UIGraphicsEndImageContext();

            

//            //1.performSelectorOnMainThread回到主线程

//            [self performSelectorOnMainThread:@selector(setImage:) withObject:fullImage waitUntilDone:NO];

            

//            //2.GCD回到主线程

//            dispatch_async(dispatch_get_main_queue(), ^{

//                _imageView = [[UIImageView alloc] init];

//                [_imageView setFrame:CGRectMake(0, 0, fullImage.size.width*0.4, fullImage.size.height*0.4)];

//                [self.view addSubview:_imageView];

//                [_imageView setImage:fullImage];

//            });

            

            //3.[NSOperationQueue mainQueue]回到主线程

            NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];

            [mainQueue addOperationWithBlock:^{

                _imageView = [[UIImageView alloc] init];

                [_imageView setFrame:CGRectMake(0, 0, fullImage.size.width*0.4, fullImage.size.height*0.4)];

                [self.view addSubview:_imageView];

                [_imageView setImage:fullImage];

            }];

        }

        

    }];

    [queue addOperation:operation];


}

//label转换成图片

-(UIImage *)getImageFromLabel:(UILabel *)label

{

    //开启上下文

    UIGraphicsBeginImageContext(label.frame.size);

    //获取当前上下文

    CGContextRef currentContent=UIGraphicsGetCurrentContext();

    [label.layer drawInContext:currentContent];

    //获得当前上下文中绘制的图片

    UIImage *labelImage=UIGraphicsGetImageFromCurrentImageContext();

    

    //结束上下文

    UIGraphicsEndImageContext();

    return labelImage;

}

- (void)setImage:(UIImage *)image

{

    NSLog(@"setImage ---%@",[NSThread currentThread]);

    _imageView = [[UIImageView alloc] init];

    [_imageView setFrame:CGRectMake(0, 0, image.size.width*0.4, image.size.height*0.4)];

    [self.view addSubview:_imageView];

    [_imageView setImage:image];

    

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值