圖片水印.擦除 圖片截屏.........

畫板

#import "DrawView.h"

@interface DrawView ()
@property(nonatomic,retain)NSMutableArray * paths;
@property(nonatomic,retain)UIBezierPath *path;
@end
@implementation DrawView
-(NSMutableArray*)paths
{
    if (!_paths)
    {
        _paths=[NSMutableArray array];
    }
    return _paths;
}
-(instancetype)initWithFrame:(CGRect)frame
{
    if (self==[super initWithFrame:frame])
    {
        UIPanGestureRecognizer*pan=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
        [self addGestureRecognizer:pan];
    }
    return self;
}
-(void)pan:(UIPanGestureRecognizer*)pan
{
    //獲取當(dāng)前手指觸摸點(diǎn)
    CGPoint currentP = [pan locationInView:self];
    //獲取開始點(diǎn)
    if (pan.state == UIGestureRecognizerStateBegan)
    {
        _path = [UIBezierPath bezierPath];
        //設(shè)置路徑的起點(diǎn)
        [_path moveToPoint:currentP];
        //保存描述好的路徑
        [self.paths addObject:_path];
    }
    // 手指拖動(dòng) 添加線到當(dāng)前觸摸點(diǎn)
    [_path addLineToPoint:currentP];
    //重繪
    [self setNeedsDisplay];
}
//只要調(diào)用這個(gè)方法买置,就會(huì)把之前繪制的東西全部清掉笆载,重新繪制
- (void)drawRect:(CGRect)rect
{
    for (UIBezierPath*path in self.paths)
    {
        [path stroke];
    }
    
}

圖片裁剪

@interface CompanyViewController ()
@property(nonatomic,retain)UIImageView*imageView;
@property(nonatomic,assign)CGPoint startP;
@property(nonatomic,retain)UIView*clipView;
@end

@implementation CompanyViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];
    self.title=@"圖片裁剪";
    _imageView=[[UIImageView alloc]initWithFrame:self.view.bounds];
    _imageView.image=[UIImage imageNamed:@"qidong"];
    [self.view addSubview:_imageView];
    _clipView=[[UIView alloc]init];
    _clipView.backgroundColor=[[UIColor blackColor] colorWithAlphaComponent:0.3];
    [self.view addSubview:_clipView];
    UIPanGestureRecognizer*pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
    [self.view addGestureRecognizer:pan];
}
-(void)pan:(UIPanGestureRecognizer*)pan
{
    if (pan.state == UIGestureRecognizerStateBegan)
    {
        _startP = [pan locationInView:_imageView];
    }
    else if (pan.state == UIGestureRecognizerStateChanged)
    {
        CGPoint endA = [pan locationInView:_imageView];
        
        CGFloat w = endA.x - _startP.x;
        CGFloat h = endA.y - _startP.y;
        //獲取截取范圍
        CGRect clipRect = CGRectMake(_startP.x, _startP.y, w, h);
        //生成截屏的view
        self.clipView.frame = clipRect;
    }
    else if (pan.state == UIGestureRecognizerStateEnded)
    {
        //圖片裁剪贮缅,生成一張新的圖片
        //開啟上下文
        //如果不透明,默認(rèn)超出裁剪區(qū)域會(huì)變成黑色惦银,通常都是透明
        UIGraphicsBeginImageContextWithOptions(_imageView.bounds.size, NO, 0);
        //設(shè)置裁剪區(qū)域
        UIBezierPath *path = [UIBezierPath bezierPathWithRect:_clipView.frame];
        
        [path addClip];
        
        //獲取上下文
        CGContextRef context = UIGraphicsGetCurrentContext();
        
        //把控件上的內(nèi)容渲染到上下文
        [_imageView.layer renderInContext:context];
        
        //生成一張新的圖片
        _imageView.image = UIGraphicsGetImageFromCurrentImageContext();
        //關(guān)閉
        UIGraphicsEndImageContext();
        
    }
    
}

圖片擦除

@interface CompanyViewController ()
@property(nonatomic,retain)UIImageView*imageView;
@end

@implementation CompanyViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];
    self.title=@"圖片水印";
    _imageView=[[UIImageView alloc]initWithFrame:self.view.bounds];
    _imageView.image=[UIImage imageNamed:@""];
    [self.view addGestureRecognizer:_imageView];
    UIPanGestureRecognizer*pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
    [self.view addGestureRecognizer:pan];
}
-(void)pan:(UIPanGestureRecognizer*)pan
{
    //獲取當(dāng)前點(diǎn)
    CGPoint curentP = [pan locationInView:self.view];
    //獲取擦除的矩形范圍
    CGFloat wh = 30;
    CGFloat x  = curentP.x - wh*0.5;
    CGFloat y  = curentP.y - wh*0.5;
    
    CGRect rect = CGRectMake(x, y, wh, wh);
    //開啟上下文
    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, 0);
    //獲取上下文
    CGContextRef context = UIGraphicsGetCurrentContext();
    //控件的layer渲染上去
    [_imageView.layer renderInContext:context];
    //擦除圖片
    CGContextClearRect(context, rect);
    //生存一張圖片
    UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
    _imageView.image = image;
    
    //關(guān)閉上下文
    UIGraphicsEndImageContext();
}

方形圖片生產(chǎn)圓形頭像

+(UIImage*)imageWithCilpImage:(UIImage*)image borderWidth:(CGFloat)borderWidth borderColor:(UIColor*)color
{
    //圖片的寬度和高度
    CGFloat imageWH = image.size.width;
    
    //圓形的寬度和高度
    CGFloat ovalWH = imageWH + 2*borderWidth;
    
    //開啟上下文
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(ovalWH, ovalWH), NO, 0);
    
    //畫大圓
    UIBezierPath * path=[UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, ovalWH, ovalWH)];
    //設(shè)置圓環(huán)顏色
    [color set];
    //顏色填充
    [path fill];
    //設(shè)置裁剪區(qū)域
    UIBezierPath *clipPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(borderWidth, borderWidth, imageWH, imageWH)];
    //圖片剪切
    [clipPath addClip];
    //繪制圖片
    [image drawAtPoint:CGPointMake(borderWidth, borderWidth)];
    
    //獲取圖片
    UIImage *clipImage = UIGraphicsGetImageFromCurrentImageContext();
    //關(guān)閉上下文
    UIGraphicsEndImageContext();
    
    return clipImage;
}

手機(jī)截屏

+(UIImage*)imageWithCaputureView:(UIView*)view
{
    //開啟位圖上下文
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, 0);
    //獲取上下文
    CGContextRef context = UIGraphicsGetCurrentContext();
    //把控件上的圖層渲染到上下文,layer只能渲染
    [view.layer renderInContext:context];
    //生成一張圖片
    UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
    //關(guān)閉上下文
    UIGraphicsEndImageContext();
    
    return image;
}

圖片水印

+(UIImage*)imageWithWaterImage:(UIImage*)image waterTitle:(NSString*)title waterAttributes:(NSDictionary*)attributes
{
    //0.獲取上下文,之前的上下文都是在view的drawRect方法中獲取(跟view相關(guān)聯(lián)的上下文layer上下文)
    //此時(shí)我們需要繪制圖片到新的圖片上僧诚,因此需要用到位圖上下文
    //怎么獲取位圖上下文,注意位圖上下文的獲取方式跟layer上下文不一樣蝗碎,位圖上下文需要我們手動(dòng)創(chuàng)建湖笨。
    //開啟一個(gè)位圖上下文,注意位圖上下文跟view無關(guān)聯(lián)蹦骑,所以不需要drawRect
    //size:位圖上下文的尺寸(新圖片的尺寸)
    //opaque:不透明度 YES:不透明 NO:透明  一般都是透明的上下文
    //scale:縮放比例 0:不縮放
    UIGraphicsBeginImageContextWithOptions(image.size,NO, 0);
    
    //1.繪制原生的圖片
    [image drawAtPoint:CGPointZero];
    //2.給原生的圖片添加文字
    NSString*str = title;
    
    [str drawAtPoint:CGPointMake(image.size.width-80, image.size.height-30) withAttributes:attributes];
    //3.從上下文獲取生成的圖片
    UIImage*imageWater = UIGraphicsGetImageFromCurrentImageContext();
    
    //4.關(guān)閉上下文
    UIGraphicsEndImageContext();
    
    return imageWater;
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末慈省,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子眠菇,更是在濱河造成了極大的恐慌边败,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,820評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件琼锋,死亡現(xiàn)場(chǎng)離奇詭異放闺,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)缕坎,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,648評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門怖侦,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人谜叹,你說我怎么就攤上這事匾寝。” “怎么了荷腊?”我有些...
    開封第一講書人閱讀 168,324評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵艳悔,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我女仰,道長(zhǎng)猜年,這世上最難降的妖魔是什么抡锈? 我笑而不...
    開封第一講書人閱讀 59,714評(píng)論 1 297
  • 正文 為了忘掉前任,我火速辦了婚禮乔外,結(jié)果婚禮上床三,老公的妹妹穿的比我還像新娘。我一直安慰自己杨幼,他們只是感情好撇簿,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,724評(píng)論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著差购,像睡著了一般四瘫。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上欲逃,一...
    開封第一講書人閱讀 52,328評(píng)論 1 310
  • 那天找蜜,我揣著相機(jī)與錄音,去河邊找鬼稳析。 笑死锹杈,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的迈着。 我是一名探鬼主播,決...
    沈念sama閱讀 40,897評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼邪码,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼裕菠!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起闭专,我...
    開封第一講書人閱讀 39,804評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤奴潘,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后影钉,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體画髓,經(jīng)...
    沈念sama閱讀 46,345評(píng)論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,431評(píng)論 3 340
  • 正文 我和宋清朗相戀三年平委,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了奈虾。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,561評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡廉赔,死狀恐怖肉微,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情蜡塌,我是刑警寧澤碉纳,帶...
    沈念sama閱讀 36,238評(píng)論 5 350
  • 正文 年R本政府宣布,位于F島的核電站馏艾,受9級(jí)特大地震影響劳曹,放射性物質(zhì)發(fā)生泄漏奴愉。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,928評(píng)論 3 334
  • 文/蒙蒙 一铁孵、第九天 我趴在偏房一處隱蔽的房頂上張望锭硼。 院中可真熱鬧,春花似錦库菲、人聲如沸账忘。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,417評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)鳖擒。三九已至,卻和暖如春烫止,著一層夾襖步出監(jiān)牢的瞬間蒋荚,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,528評(píng)論 1 272
  • 我被黑心中介騙來泰國(guó)打工馆蠕, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留期升,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,983評(píng)論 3 376
  • 正文 我出身青樓互躬,卻偏偏與公主長(zhǎng)得像播赁,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子吼渡,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,573評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容