Quartz2d图片裁剪/加水印/截屏

来源:互联网 发布:傻瓜进销存软件 编辑:程序博客网 时间:2024/05/01 00:17

绘制图片和文字不需要手动获取图形上下文,只要开启图形上下文就获取了.

图片和文字可以直接绘制到图形上下文中,

如果图形上下文中绘制了路径并执行了裁剪,那么只有路径区域会显示图片;

图片的显示形状与路径的形状有关.执行裁剪实际上就是把图形上下文裁剪成了路径的形状.


======================================

    //1.加载要裁剪的图片

    UIImage * image = [UIImageimageNamed:@"dst2"];

    

    //2.开启一个图形上下文 (bitmap 大小和要裁剪的图片大小一样)

    UIGraphicsBeginImageContextWithOptions(image.size,NO,0.0);//参数:上下文区域大小,是否透明,缩放率

    

    //3.获取图形上下文

    CGContextRef ctx =UIGraphicsGetCurrentContext();

    

    //4.创建路径

    //设置圆心

     CGPoint centerP = CGPointMake(image.size.width/2, image.size.height/2);

    //设置半径

    CGFloat radius = MIN(image.size.width, image.size.height)/2;

    

    UIBezierPath * path = [UIBezierPathbezierPathWithArcCenter:centerPradius:radiusstartAngle:0endAngle:2 *M_PI clockwise:YES];

    

    //5.把路径添加到图形上下文中

    CGContextAddPath(ctx, path.CGPath);

    

    //6.执行裁剪

    CGContextClip(ctx);

    

    

    //7.绘制图片

   

    [image drawAtPoint:CGPointZero];

    

    //8.获取图片

    UIImage * getImage =UIGraphicsGetImageFromCurrentImageContext();

    

    //8.1 结束图形上下文

    UIGraphicsEndImageContext();

//

//    //8.2 裁剪图片

//    

//    CGFloat x = 0;

//    CGFloat y = (image.size.height - 2 * radius)/2;

//    

//    CGFloat w = 2 * radius;

//    CGFloat h = w;

//    

//    //获取屏幕的缩放比

//    CGFloat scale = [UIScreen mainScreen].scale;

//    x *= scale;

//    y *= scale;

//    

//    w *= scale;

//    h *= scale;

//    

//    CGImageRef imageRef = CGImageCreateWithImageInRect(getImage.CGImage, CGRectMake(x, y, w, h));

//

////    获取裁剪后的图片

//    getImage = [UIImage imageWithCGImage:imageRef];

//    

    self.imageView.image = getImage;

    

    //9.保存

    

    //9.1 保存相册

    UIImageWriteToSavedPhotosAlbum(getImage, self, @selector(image:didFinishSavingWithError:contextInfo:),@"hello word");

    //9.2 保存沙盒

    

    NSString * documents = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)lastObject];

    

    //拼接文件名

    NSString * fileName = [documents stringByAppendingPathComponent:@"001.png"];

    

    //UIimage--->NSData

    NSData * imageData = UIImagePNGRepresentation(getImage);

    

    [imageData writeToFile:fileName atomically:YES];

    

    NSLog(@"%@",fileName);

}

//保存相册方法

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo

{

    NSLog(@"保存完毕 %@",contextInfo);

}


==============================

//1.加载要添加水印图片

    UIImage * image = [UIImageimageNamed:@"img05"];

    

    //2.开启和图片大小相同的图形上下文(bitmap)

    UIGraphicsBeginImageContextWithOptions(image.size,NO,0.0);

    

    

    //3.把图片绘制到刚刚开启的图形上下文中

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

    

    

    //4.添加文字水印

    NSString * text =@"美人你一直是我的春天,你是我的世外桃源";

    

    NSDictionary * attrs =@{NSForegroundColorAttributeName:[UIColorredColor],

                             NSFontAttributeName:[UIFontsystemFontOfSize:20.f]

                             };

    [text drawInRect:CGRectMake(200,500,300,200)withAttributes:attrs];

    

    

    

    //4.1 添加图片水印

    //加载水印图片

    UIImage * logoImage = [UIImageimageNamed:@"logo"];

    

    [logoImage drawInRect:CGRectMake(300,700,100,38)];

    

    //5.从图形上下文中获取图片

    UIImage * getImage =UIGraphicsGetImageFromCurrentImageContext();

    

    //5.1 结束上下文

    UIGraphicsEndImageContext();

    

    //6.保存图片到相册

    UIImageWriteToSavedPhotosAlbum(getImage,nil,nil,nil);


========================================


截屏

        //1.开启一个图形上下文

        UIGraphicsBeginImageContextWithOptions(self.view.frame.size,NO,0.0);

        

        //2.获取图形上下文对象

        CGContextRef ctx =UIGraphicsGetCurrentContext();

        

        //3.调用view属性layer的方法 renderIncontex 把内容绘制到图形上下文中

        [self.view.layerrenderInContext:ctx];

        

        //4.获取图片

        UIImage * getImage =UIGraphicsGetImageFromCurrentImageContext();

        

        //5.结束图形上下文

        UIGraphicsEndImageContext();

        

        //6.保存图片

        UIImageWriteToSavedPhotosAlbum(getImage,nil,nil, nil);


截屏方法二(只能在设备上运行,模拟器会报错):

CGImageRef UIGetScreenImage(); 
    CGImageRef img = UIGetScreenImage();
    UIImage* scImage=[UIImage imageWithCGImage:img];
    UIImageWriteToSavedPhotosAlbum(scImage, nil, nil, nil);



0 0
原创粉丝点击