iOS-圖片水印,圖片裁剪和螢幕截圖

weixin_34075551發表於2016-09-23

一.圖片水印

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

相關文章