UIGraphicsBeginImageContextWithOptions

簡(jiǎn)介

圖片上下文,圖片上下文的繪制不需要在drawRect:方法中進(jìn)行,在一個(gè)普通的OC方法中就可以繪制。

獲取圖片上下文

使用兩個(gè)方法同樣都可以創(chuàng)建,但是使用第一個(gè)方法將來(lái)創(chuàng)建的圖片清晰度和質(zhì)量沒(méi)有第二種方法的好。

// 參數(shù): 指定將來(lái)創(chuàng)建出來(lái)的bitmap的大小
UIGraphicsBeginImageContext(CGSize size);
/*
 * 參數(shù)一: 指定將來(lái)創(chuàng)建出來(lái)的bitmap的大小
 * 參數(shù)二: 設(shè)置透明YES代表透明,NO代表不透明
 * 參數(shù)三: 代表縮放,0代表不縮放
 */
UIGraphicsBeginImageContextWithOptions(CGSize size, BOOL opaque, CGFloat scale);
使用步驟
  • 獲取圖片上下文
  • 繪圖
  • 從圖片上下文中獲取繪制的圖片
  • 關(guān)閉圖片上下文
- (UIImage *)drawImageWithImageNamed:(NSString *)name {
    // 獲取圖片
    UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:name ofType:nil]];
    // 1.開(kāi)啟圖形上下文
    UIGraphicsBeginImageContext(image.size);
    // 2.繪制到圖形上下文中
    [image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
    // 3.從上下文中獲取圖片
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    // 4.關(guān)閉圖形上下文
    UIGraphicsEndImageContext();
    //返回圖片
    return newImage;
}

實(shí)際應(yīng)用

圖片添加水印
  • 添加文字水印
- (UIImage *)waterImageWithImage:(UIImage *)image text:(NSString *)text textPoint:(CGPoint)point attributedString:(NSDictionary * )attributed {
    //1.開(kāi)啟上下文 CGSize size 尺寸, BOOL opaque 透明度, CGFloat scale 比例
    UIGraphicsBeginImageContextWithOptions(image.size, NO, 0);
    //2.繪制圖片
    [image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
    //添加水印文字
    [text drawAtPoint:point withAttributes:attributed];
    //3.從上下文中獲取新圖片
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    //4.關(guān)閉圖形上下文
    UIGraphicsEndImageContext();
    //返回圖片
    return newImage;
}

加載圖片

UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
// 原始圖片
UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"img_meizi.jpg" ofType:nil]];
// 文字屬性
NSDictionary *attributed = @{NSFontAttributeName: [UIFont systemFontOfSize:50.0f], NSForegroundColorAttributeName: [UIColor redColor]};
// 添加水印
imageView.image = [self waterImageWithImage:image text:@"添加一個(gè)水印" textPoint:CGPointMake(50, 50) attributedString:attributed];
[self.view addSubview:imageView];
  • 添加圖片水印
- (UIImage *)waterImageWithImage:(UIImage *)image waterImage:(UIImage *)waterImage waterImageRect:(CGRect)rect {
    //1.開(kāi)啟上下文
    UIGraphicsBeginImageContextWithOptions(image.size, NO, 0);
    //2.繪制背景圖片
    [image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
    //3.繪制水印圖片到當(dāng)前上下文
    [waterImage drawInRect:rect];
    //4.從上下文中獲取新圖片
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    //5.關(guān)閉圖形上下文
    UIGraphicsEndImageContext();
    //返回圖片
    return newImage;
}

加載圖片

UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
// 原始圖片
UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"img_meizi.jpg" ofType:nil]];
// 水印圖片
UIImage *waterImg = [UIImage imageNamed:@"chat_send_nor"];
// 添加水印
imageView.image = [self waterImageWithImage:image waterImage:waterImg waterImageRect:CGRectMake(50, 50, 64, 58)];
[self.view addSubview:imageView];
圖片裁剪
  • 裁剪圓形圖片
- (UIImage *)clipCircleImageWithImage:(UIImage *)image circleRect:(CGRect)rect {
    //1、開(kāi)啟上下文
    UIGraphicsBeginImageContextWithOptions(image.size, NO, 0);
    //2、設(shè)置裁剪區(qū)域
    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:rect];
    //裁剪
    [path addClip];
    //3、繪制圖片
    [image drawAtPoint:CGPointZero];
    //4、獲取新圖片
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    //5、關(guān)閉上下文
    UIGraphicsEndImageContext();
    //6、返回新圖片
    return newImage;
}
  • 裁剪邊框圖片
- (UIImage *)clipCircleImageWithImage:(UIImage *)image circleRect:(CGRect)rect borderWidth:(CGFloat)borderW borderColor:(UIColor *)borderColor {
    //1、開(kāi)啟上下文
    UIGraphicsBeginImageContext(image.size);
    //2、設(shè)置邊框
    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:rect];
    //填充邊框
    [borderColor setFill];
    [path fill];
    //3、設(shè)置裁剪區(qū)域
    UIBezierPath *clipPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(rect.origin.x + borderW , rect.origin.x + borderW , rect.size.width - borderW * 2, rect.size.height - borderW *2)];
    [clipPath addClip];
    //3、繪制圖片
    [image drawAtPoint:CGPointZero];
    //4、獲取新圖片
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    //5、關(guān)閉上下文
    UIGraphicsEndImageContext();
    //6、返回新圖片
    return newImage;
}
截屏
+ (void)cutScreenWithView:(nullable UIView *)view successBlock:(nullable void(^)(UIImage * _Nullable image,NSData * _Nullable imagedata))block {
    //1、開(kāi)啟上下文
    UIGraphicsBeginImageContext(view.bounds.size);
    //2.獲取當(dāng)前上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    //3.截屏
    [view.layer renderInContext:ctx];
    //4、獲取新圖片
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    //5.轉(zhuǎn)化成為Data
    //compressionQuality:表示壓縮比 0 - 1的取值范圍
    NSData *data = UIImageJPEGRepresentation(newImage, 1);
    //6、關(guān)閉上下文
    UIGraphicsEndImageContext();
    //7.回調(diào)
    block(newImage, data);
}
擦除
- (UIImage *)wipeImageWithView:(UIView *)view currentPoint:(CGPoint)point size:(CGSize)size {
    //計(jì)算位置
    CGFloat offsetX = point.x - size.width * 0.5;
    CGFloat offsetY = point.y - size.height * 0.5;
    CGRect clipRect = CGRectMake(offsetX, offsetY, size.width, size.height);
    //開(kāi)啟上下文
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, 0);
    //獲取當(dāng)前的上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    //把圖片繪制上去
    [view.layer renderInContext:ctx];
    //把這一塊設(shè)置為透明
    CGContextClearRect(ctx, clipRect);
    //獲取新的圖片
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    //關(guān)閉上下文
    UIGraphicsEndImageContext();
    //重新賦值圖片
    return newImage;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Core Graphics Framework是一套基于C的API框架,使用了Quartz作為繪圖引擎。它提供了低...
    ShanJiJi閱讀 1,736評(píng)論 0 20
  • --繪圖與濾鏡全面解析 概述 在iOS中可以很容易的開(kāi)發(fā)出絢麗的界面效果,一方面得益于成功系統(tǒng)的設(shè)計(jì),另一方面得益...
    韓七夏閱讀 2,981評(píng)論 2 10
  • 原文地址:http://www.cocoachina.com/industry/20140115/7703.htm...
    默默_David閱讀 6,260評(píng)論 0 1
  • 《影子》 微信名:Sophia(數(shù)學(xué)劉老師) 筆名:安然 “影子,如果沒(méi)有你,我大概會(huì)孤獨(dú)終老吧?”我這樣對(duì)影子說(shuō)...
    Sophia安然閱讀 442評(píng)論 2 2
  • 春在家排行第六,是老幺,上有三個(gè)姐姐兩個(gè)哥哥。即使出生在三年自然災(zāi)害時(shí)代,也沒(méi)挨著什么餓。窮人的孩子早當(dāng)家,這都是...
    山腳下的小葉子閱讀 717評(píng)論 2 2

友情鏈接更多精彩內(nèi)容