iOS-图片水印,图片裁剪和屏幕截图

2016-09-23  本文已影响0人  Mn_Su

一.图片水印

    1.创建个UIImageView
        @property (weak, nonatomic) IBOutlet UIImageView *neImage;
    
    2.创建个方法实现水印功能
        - (void)viewDidLoad {
            [super viewDidLoad];
        
            UIImage *bgImage = [UIImage imageNamed:@""];
            
            //创建一个位图上下文
            UIGraphicsBeginImageContextWithOptions(bgImage.size, NO, 0.0);
            
            //将背景图片画入位图中
            [bgImage drawInRect:CGRectMake(0, 0, bgImage.size.width, bgImage.size.height)];
            
            //将水印Logo画入背景图中
            UIImage *waterIma = [UIImage imageNamed:@""];
            [waterIma drawInRect:CGRectMake(bgImage.size.width - 40 - 5, bgImage.size.height - 40 - 5, 40, 40)];
            
            //取得位图上下文中创建的新的图片
            UIImage *newimage = UIGraphicsGetImageFromCurrentImageContext();
            
            //结束上下文
            UIGraphicsEndImageContext();    

            //在创建的ImageView上显示出新图片
            self.neImage.image = newimage;
            
            //压缩新照片为PNG格式的二进制数据
            NSData *data = UIImagePNGRepresentation(newimage);
            
            //将图片写入到手机存储中
            NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"new.png"];
            [data writeToFile:path atomically:YES];
            
    }

二.图片裁剪

    //1.加载原图
        UIImage *oldImage = [UIImage imageNamed:@"me"];
        
        //2.获取位图上下文
        CGFloat bigCic = oldImage.size.width + 2 * 2;
        UIGraphicsBeginImageContextWithOptions(CGSizeMake(bigCic, bigCic), NO, 0.0);
        
        //3.画大圆
        [[UIColor whiteColor] set];
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        CGContextAddArc(ctx, bigCic * 0.5, bigCic * 0.5, bigCic * 0.5, 0, M_PI * 2, 0);
        CGContextFillPath(ctx);
        
        //4.画小圆
        CGFloat smallCic = oldImage.size.width * 0.5;
        CGContextAddArc(ctx, bigCic * 0.5 , bigCic * 0.5, smallCic, 0, M_PI * 2, 0);
        CGContextClip(ctx);
        
        //5.画图
        [oldImage drawInRect:CGRectMake(2, 2, oldImage.size.width, oldImage.size.height)];
        
        //6.获取新图
        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
        
        //7.结束上下文
        UIGraphicsEndImageContext();
        
        //8.显示新图
        self.IconView.image = newImage;
        
        //9.写入到手机存储
        NSData *data = UIImagePNGRepresentation(newImage);
        NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"newClip.png"];
        [data writeToFile:path atomically:YES];

三,屏幕截图

 //1.开启位图上下文
UIGraphicsBeginImageContextWithOptions(self.view.frame.size, NO, 0.0);

//2.渲染截图
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];

//3.获取新图
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

//4.写入到手机存储
NSData *data = UIImagePNGRepresentation(newImage);
NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"newClip.png"];
[data writeToFile:path atomically:YES];

//5.关闭上下文
UIGraphicsEndImageContext();
上一篇下一篇

猜你喜欢

热点阅读