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

释放双眼,带上耳机,听听看~!

1.图片水印

- (void)getnewImage {
    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.newImage = newimage;
    
    //压缩新照片为PNG格式的二进制数据
    NSData *data = UIImagePNGRepresentation(newimage);
    
    //将图片写入到手机存储中
    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"new.png"];
    [data writeToFile:path atomically:YES];
    
}

2.图片裁剪

//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];

3.屏幕截图

//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();

 

人已赞赏
iOS文章

iOS获取手机健康步数,去除人为添加的数据

2019-11-2 9:41:57

iOS文章

探讨iOS开发中各种锁

2019-11-2 11:37:14

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索