QuartZ2D_03

MyImageView

#import "MyImageView.h"

@implementation MyImageView


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.

/** uiimageview這個(gè)類不會(huì)自動(dòng)畫線    image更改顯示不出 要在控制器里寫*/
//- (void)drawRect:(CGRect)rect {
//    // Drawing code
//    UIBezierPath *path = [UIBezierPath bezierPath];
//    [path moveToPoint:CGPointMake(50, 50)];
//    [path addLineToPoint:CGPointMake(100, 100)];
//    path.lineWidth = 20;
//    [path stroke];
//    
//    
//}


@end

MyView.h

#import <UIKit/UIKit.h>
typedef void (^PathBlock)(UIBezierPath *p);
@interface MyView : UIView
@property(nonatomic,copy)PathBlock pb;
@end

MyView.m

#import "MyView.h"

@interface MyView ()

@property(nonatomic,strong)NSMutableArray *pathArray;
@end
@implementation MyView
/** 懶加載pathArray */
- (NSMutableArray *)pathArray{
    if (!_pathArray) {
        _pathArray = [NSMutableArray array];
    }
    return _pathArray;
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    // 1. 獲取起始點(diǎn) 并且創(chuàng)建路徑
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:[[touches anyObject] locationInView:self]];
    // 2. 裝入數(shù)組
    [self.pathArray addObject:path];
    
}
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    // 1. 獲取終點(diǎn) (不止一個(gè))
    UIBezierPath *path = [self.pathArray lastObject];
    [path addLineToPoint:[[touches anyObject] locationInView:self]];
    // 2. 調(diào)用drawRect(drawrect不會(huì)自動(dòng)調(diào)用 需要使用以下方法)
    [self setNeedsDisplay];
 
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
   // 封閉路徑
    UIBezierPath *path = [self.pathArray lastObject];
    [path closePath];

    [self setNeedsDisplay];
    //調(diào)用block傳遞路徑
   self.pb(path);
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
    for (UIBezierPath *path in self.pathArray) {
        path.lineWidth = 2;
        [path stroke];
    }
}


@end

ViewController.m

#import "ViewController.h"
#import "MyImageView.h"
#import "MyView.h"
@interface ViewController ()
@property(nonatomic,strong)UIImageView *imageView;
@property(nonatomic,strong)MyView *mv;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
// 獲取圖片
   __block UIImage *image = [UIImage imageNamed:@"1.png"];
    
    self.imageView = [[UIImageView alloc]initWithImage:image];
    [self.view addSubview:self.imageView];
    self.mv = [[MyView alloc]initWithFrame:self.imageView.frame];
    self.mv.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0]; //這種透明方式 不會(huì)讓子視圖也變透明(alpha是直接設(shè)置會(huì)讓子視圖也透明)
    [self.view addSubview:self.mv
     ];
    //剪切
    __weak typeof(self) weakSelf = self;
    self.mv.pb = ^(UIBezierPath *p){
    
        //獲取上下文
        UIGraphicsBeginImageContextWithOptions(image.size, NO, 0);
        //剪切路徑
        UIBezierPath *path = p;
        //剪切
        [path addClip];
        //繪制
        [image drawAtPoint:CGPointZero];
        // 4 .需要接收剪切之后的圖
        image = UIGraphicsGetImageFromCurrentImageContext();
        // 5. 關(guān)閉上下文
        UIGraphicsEndImageContext();
        //顯示出來剪切的圖
        weakSelf.imageView.image = image;
    
        
        
    };
}



/******** 剪切一張圖 ************/
- (void)clipImage{

    // 1. 擁有一張圖
    UIImage *image = [UIImage imageNamed:@"1.png"];
    MyImageView *myImageView = [[MyImageView alloc]initWithImage:image];
    
    CGRect rect = CGRectMake(50, 50, 0, 0);
    rect.size = image.size;
    myImageView.frame = rect;
    //以上是剪切之前的圖片顯示
    //獲取上下文
    UIGraphicsBeginImageContextWithOptions(image.size, NO, 0);
    // 2. 需要剪裁的范圍
    //    UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(10, 10, 50, 50)];
    //剪切個(gè)圓
    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
    // 3. 剪切 原來的圖不變
    [path addClip];
    //繪制
    [image drawAtPoint:CGPointZero];
    // 4 .需要接收剪切之后的圖
    UIImage *clipImage = UIGraphicsGetImageFromCurrentImageContext();
    // 5. 關(guān)閉上下文
    UIGraphicsEndImageContext();
    //顯示出來剪切的圖
    myImageView.image = clipImage;
        
    [self.view addSubview:myImageView];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 211,423評(píng)論 6 491
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異囚衔,居然都是意外死亡眷射,警方通過查閱死者的電腦和手機(jī)匙赞,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,147評(píng)論 2 385
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來妖碉,“玉大人涌庭,你說我怎么就攤上這事∨芬耍” “怎么了坐榆?”我有些...
    開封第一講書人閱讀 157,019評(píng)論 0 348
  • 文/不壞的土叔 我叫張陵,是天一觀的道長冗茸。 經(jīng)常有香客問我席镀,道長匹中,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 56,443評(píng)論 1 283
  • 正文 為了忘掉前任豪诲,我火速辦了婚禮顶捷,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘屎篱。我一直安慰自己服赎,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,535評(píng)論 6 385
  • 文/花漫 我一把揭開白布交播。 她就那樣靜靜地躺著重虑,像睡著了一般。 火紅的嫁衣襯著肌膚如雪秦士。 梳的紋絲不亂的頭發(fā)上缺厉,一...
    開封第一講書人閱讀 49,798評(píng)論 1 290
  • 那天,我揣著相機(jī)與錄音隧土,去河邊找鬼芽死。 笑死,一個(gè)胖子當(dāng)著我的面吹牛次洼,可吹牛的內(nèi)容都是我干的关贵。 我是一名探鬼主播,決...
    沈念sama閱讀 38,941評(píng)論 3 407
  • 文/蒼蘭香墨 我猛地睜開眼卖毁,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼揖曾!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起亥啦,我...
    開封第一講書人閱讀 37,704評(píng)論 0 266
  • 序言:老撾萬榮一對(duì)情侶失蹤炭剪,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后翔脱,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體奴拦,經(jīng)...
    沈念sama閱讀 44,152評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,494評(píng)論 2 327
  • 正文 我和宋清朗相戀三年届吁,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了错妖。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,629評(píng)論 1 340
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡疚沐,死狀恐怖暂氯,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情亮蛔,我是刑警寧澤痴施,帶...
    沈念sama閱讀 34,295評(píng)論 4 329
  • 正文 年R本政府宣布,位于F島的核電站,受9級(jí)特大地震影響辣吃,放射性物質(zhì)發(fā)生泄漏动遭。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,901評(píng)論 3 313
  • 文/蒙蒙 一神得、第九天 我趴在偏房一處隱蔽的房頂上張望厘惦。 院中可真熱鬧,春花似錦循头、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,742評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至形入,卻和暖如春全跨,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背亿遂。 一陣腳步聲響...
    開封第一講書人閱讀 31,978評(píng)論 1 266
  • 我被黑心中介騙來泰國打工浓若, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人蛇数。 一個(gè)月前我還...
    沈念sama閱讀 46,333評(píng)論 2 360
  • 正文 我出身青樓挪钓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親耳舅。 傳聞我的和親對(duì)象是個(gè)殘疾皇子碌上,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,499評(píng)論 2 348

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