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

iOS开发 专栏收录该内容
104 篇文章 2 订阅

1.图片水印

<span style="font-size:18px;">- (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];
    
}
</span>

2.图片裁剪

<span style="font-size:18px;"> //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];</span>

3.屏幕截图

<span style="font-size:18px;">//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();</span>


  • 1
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

相关推荐
©️2020 CSDN 皮肤主题: 成长之路 设计师:Amelia_0503 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值