圖片剪裁

做項(xiàng)目是經(jīng)常會遇見需要對圖片進(jìn)行剪裁的情況,下面來點(diǎn)干貨

#import <UIKit/UIKit.h>

@protocol PassImageDelegate <NSObject>

-(void)passImage:(UIImage *)image;

@end

@interface CutImgageVC : UIViewController
@property(nonatomic,strong) id<PassImageDelegate> delegate;
@property(nonatomic,strong) UIImage *image;
@property(nonatomic,assign) float ratioWidth;//寬
@property(nonatomic,assign) float ratioHeight;//高
@property(nonatomic,assign) float minWidth;//最小寬
@end


#import "CutImgageVC.h"

#define imgViewWigth 30
@interface CutImgageVC ()<UIGestureRecognizerDelegate>{
    float imageX;
    float imageY;
    float imageW;
    float imageH;

}
@property(nonatomic,strong) UIImageView *bgImgView;
@property(nonatomic,strong) UIView *cutView;
@property(nonatomic,strong) UIImageView *topLeftImgView;
@property(nonatomic,strong) UIImageView *topRigthImgView;
@property(nonatomic,strong) UIImageView *bottomLeftImgView;
@property(nonatomic,strong) UIImageView *bottomRigthImgView;
@end

@implementation CutImgageVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //添加導(dǎo)航欄和完成按鈕
    
    UINavigationBar *naviBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0,ScreenWidth, 64)];
    [self.view addSubview:naviBar];
    
    UINavigationItem *naviItem = [[UINavigationItem alloc] initWithTitle:@"圖片裁剪"];
    [naviBar pushNavigationItem:naviItem animated:YES];
    
    //保存按鈕
    UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(saveButton)];
    naviItem.rightBarButtonItem = doneItem;
    [self createImageView];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)saveButton{

    [self clipWithRect];
    [self dismissModalViewControllerAnimated:YES];
}

-(void)clipWithRect{
    CGRect cropFrame = self.cutView.frame;
    CGFloat orgX = (cropFrame.origin.x-imageX) * (self.image.size.width / imageW);
    CGFloat orgY = (cropFrame.origin.y-imageY) * (self.image.size.height /imageH);
    CGFloat width = cropFrame.size.width * (self.image.size.width / imageW);
    CGFloat height = width*_ratioHeight/_ratioWidth;
    CGRect cropRect = CGRectMake(orgX, orgY, width, height);
    CGImageRef imgRef = CGImageCreateWithImageInRect(self.bgImgView.image.CGImage, cropRect);
    
    CGFloat deviceScale = [UIScreen mainScreen].scale;
    UIGraphicsBeginImageContextWithOptions(cropRect.size, 0, deviceScale);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(context, 0, cropRect.size.height);
    CGContextScaleCTM(context, 1, -1);
    CGContextDrawImage(context, CGRectMake(0, 0, cropRect.size.width, cropRect.size.height), imgRef);
    UIImage *newImg = UIGraphicsGetImageFromCurrentImageContext();
    CGImageRelease(imgRef);
    UIGraphicsEndImageContext();
    [self.delegate passImage:newImg];
}

-(void)createImageView{
    [self.view addSubview:self.bgImgView];
    [self.view addSubview:self.cutView];
    [self.view addSubview:self.topLeftImgView];
    [self.view addSubview:self.topRigthImgView];
    [self.view addSubview:self.bottomLeftImgView];
    [self.view addSubview:self.bottomRigthImgView];
    
    //設(shè)置默認(rèn)值
    [self setDefaultInfo];
    
    self.cutView.origin=self.topLeftImgView.center;
}

-(void)setDefaultInfo{
    if (self.cutView.width<_minWidth) {
        self.cutView.width=_minWidth;
        self.cutView.height=self.cutView.width *_ratioHeight/_ratioWidth;
    }
    if (self.cutView.height<_minWidth *_ratioHeight/_ratioWidth) {
        self.cutView.height=_minWidth*_ratioHeight/_ratioWidth;
        self.cutView.width=_minWidth;
    }
    
    if (self.cutView.x<imageX) {
        self.cutView.x=imageX;
    }
    if (self.cutView.y<imageY){
        self.cutView.y=imageY;
    }
    if (self.cutView.x+self.cutView.width>imageW+imageX) {
        self.cutView.x=imageW+imageX-self.cutView.width;
        if (self.cutView.x<imageX) {
            self.cutView.x=imageX;
        }
        if (self.cutView.width>imageW) {
            self.cutView.width=imageW;
            self.cutView.height=self.cutView.width *_ratioHeight/_ratioWidth;
            
        }

       
    }
    if (self.cutView.y+self.cutView.height>imageH+imageY) {
        self.cutView.y=imageH+imageY-self.cutView.height;
        if (self.cutView.y<imageY){
            self.cutView.y=imageY;
        }
        if (self.cutView.height>imageH) {
            self.cutView.height=imageH;
            self.cutView.width=self.cutView.height *_ratioWidth/_ratioHeight;
        }
      
    }
    
    //更新4個(gè)imgView 的位置
    self.topLeftImgView.origin=CGPointMake(self.cutView.origin.x-imgViewWigth/2, self.cutView.origin.y-imgViewWigth/2);
    self.topRigthImgView.x=self.topLeftImgView.x+self.cutView.width;
    self.topRigthImgView.y=self.topLeftImgView.y;
    
    self.bottomLeftImgView.x=self.topLeftImgView.x;
    self.bottomLeftImgView.y=self.topLeftImgView.y+self.cutView.height;
    
    self.bottomRigthImgView.x= self.topRigthImgView.x;
    self.bottomRigthImgView.y= self.bottomLeftImgView.y;
    
}

-(void)updateFrameWithImgView{

}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    // 拿到UITouch就能獲取當(dāng)前點(diǎn)
    UITouch *touch = [touches anyObject];
    if (touch.view ==self.cutView) {
        // 獲取當(dāng)前點(diǎn)
        CGPoint curP = [touch locationInView:self.cutView];
        // 獲取上一個(gè)點(diǎn)
        CGPoint preP = [touch previousLocationInView:self.cutView];
        // 獲取手指x軸偏移量
        CGFloat offsetX = curP.x - preP.x;
        // 獲取手指y軸偏移量
        CGFloat offsetY = curP.y - preP.y;
        // 移動(dòng)當(dāng)前view
        self.cutView.transform = CGAffineTransformTranslate(self.cutView.transform, offsetX, offsetY);
        
        
     [self setDefaultInfo];
    }
}


- (void)handleTDTap:(UIPanGestureRecognizer *)theTDTap{
    if (theTDTap.state==UIGestureRecognizerStateChanged) {
        UIImageView *imgView=(UIImageView *)[theTDTap view];
        CGPoint point = [theTDTap locationInView:self.view];
        
        CGPoint translation = [theTDTap translationInView:theTDTap.view];
        CGFloat absX = fabs(translation.x);
        CGFloat absY = fabs(translation.y);
        // 設(shè)置滑動(dòng)有效距離
        if (MAX(absX, absY) < 10)
            return;
        //根據(jù)拖動(dòng)點(diǎn)的Origin和比例更新當(dāng)前框的大小
        if (absX>absY) {
            //橫滑
            NSLog(@"123");
            [self slideWithHorizontally:imgView andPoint:point];
        }else if (absX<absY){
            //豎滑
            NSLog(@"1234");
            [self slideWithVerticaly:imgView andPoint:point];
        }else{
            NSLog(@"12345");
        }
        [self setDefaultInfo];
    }
}

//橫滑判斷
-(void)slideWithHorizontally:(UIImageView *)imgView andPoint:(CGPoint)point{

    if (imgView ==self.topLeftImgView) {
        //如果為第一個(gè)點(diǎn)
        imgView.origin=point;//更新當(dāng)前imgView 的位置
        //改變第二個(gè)點(diǎn)的Y
        self.topRigthImgView.y= imgView.y;
        
        //更新self.cutView frame
        self.cutView.origin=self.topLeftImgView.center;
        self.cutView.width=self.topRigthImgView.x-self.topLeftImgView.x;
       
        self.cutView.height=self.cutView.width *_ratioHeight/_ratioWidth;
        
        //改變第三個(gè)點(diǎn)的X
        self.bottomLeftImgView.x=imgView.x;
        self.bottomLeftImgView.y= imgView.y+self.cutView.height;
        
        //第4個(gè)點(diǎn)的位置
        //            self.bottomRigthImgView.x= self.topRigthImgView.x;
        self.bottomRigthImgView.y= self.bottomLeftImgView.y;
    }else if (imgView ==self.topRigthImgView){
        //如果為第二個(gè)點(diǎn)
         imgView.origin=point;//更新當(dāng)前imgView 的位置
        //改變第一個(gè)點(diǎn)的Y
        self.topLeftImgView.y=imgView.y;
        //更新self.cutView frame
        self.cutView.origin=self.topLeftImgView.center;
        self.cutView.width=self.topRigthImgView.x-self.topLeftImgView.x;
        self.cutView.height=self.cutView.width *_ratioHeight/_ratioWidth;
        
        //第3個(gè)點(diǎn)的位置
        //            self.bottomLeftImgView.x=self.topLeftImgView.x;
        self.bottomLeftImgView.y= imgView.y+self.cutView.height;
        
        //第4個(gè)點(diǎn)的位置
        self.bottomRigthImgView.x=imgView.x;
        self.bottomRigthImgView.y= self.bottomLeftImgView.y;
    }else if (imgView ==self.bottomLeftImgView){
        //如果為第3個(gè)點(diǎn)
        imgView.origin=point;//更新當(dāng)前imgView 的位置

        //更新self.cutView frame
        
        //第4個(gè)點(diǎn)的位置
        self.bottomRigthImgView.y= imgView.y;
        
        self.cutView.width=self.bottomRigthImgView.x-self.bottomLeftImgView.x;
        self.cutView.height=self.cutView.width *_ratioHeight/_ratioWidth;
        
        //第1個(gè)點(diǎn)的位置
        self.topLeftImgView.x=imgView.x;
        self.topLeftImgView.y= imgView.y-self.cutView.height;;
        
        //第2個(gè)點(diǎn)的位置
        self.topRigthImgView.y= self.topLeftImgView.y;
        
        self.cutView.origin=self.topLeftImgView.center;
        
        
    }else if (imgView ==self.bottomRigthImgView){
        //如果為第4個(gè)點(diǎn)
        imgView.origin=point;//更新當(dāng)前imgView 的位置
        //第3個(gè)點(diǎn)的位置
        self.bottomLeftImgView.y= imgView.y;
        //更新self.cutView frame
        
        self.cutView.width=self.bottomRigthImgView.x-self.bottomLeftImgView.x;
        self.cutView.height=self.cutView.width *_ratioHeight/_ratioWidth;
        
        //第1個(gè)點(diǎn)的位置
        self.topLeftImgView.y= imgView.y-self.cutView.height;
        
        //第2個(gè)點(diǎn)的位置
        self.topRigthImgView.x=imgView.x;
        self.topRigthImgView.y= self.topLeftImgView.y;
        
        self.cutView.origin=self.topLeftImgView.center;
    }else{
        return;
    }
//    [self setDefaultInfo];
    
}

-(void)slideWithVerticaly:(UIImageView *)imgView andPoint:(CGPoint)point{
   
    if (imgView ==self.topLeftImgView) {
        //如果為第一個(gè)點(diǎn)
        imgView.origin=point;//更新當(dāng)前imgView 的位置
        //更新self.cutView frame
        self.cutView.origin=self.topLeftImgView.center;
        self.cutView.height=self.bottomLeftImgView.y-self.topLeftImgView.y;
        self.cutView.width=self.cutView.height *_ratioWidth/_ratioHeight;
        
        //改變第3個(gè)點(diǎn)
        self.bottomLeftImgView.x=imgView.x;
       
        //改變第二個(gè)點(diǎn)的Y
        self.topRigthImgView.x=imgView.x+self.cutView.width;
        self.topRigthImgView.y= imgView.y;
        
        //第4個(gè)點(diǎn)的位置
        self.bottomRigthImgView.x= self.topRigthImgView.x;
        self.bottomRigthImgView.y= self.bottomLeftImgView.y;
    }else if (imgView ==self.topRigthImgView){
        //如果為第二個(gè)點(diǎn)
         imgView.origin=point;//更新當(dāng)前imgView 的位置
        //更新self.cutView frame
        self.cutView.height=self.bottomLeftImgView.y-self.topLeftImgView.y;
        self.cutView.width=self.cutView.height *_ratioWidth/_ratioHeight;
        
        //改變第4個(gè)點(diǎn)的X
        self.bottomRigthImgView.x=imgView.x;
        
        //改變第1個(gè)點(diǎn)的X
        self.topLeftImgView.x=imgView.x-self.cutView.width;
        self.topLeftImgView.y=imgView.y;
        self.cutView.origin=self.topLeftImgView.center;
        
        //第3個(gè)點(diǎn)的位置
        self.bottomLeftImgView.x=self.topLeftImgView.x;
        self.bottomLeftImgView.y= self.bottomRigthImgView.y;
        
    }else if (imgView ==self.bottomLeftImgView){
        //如果為第3個(gè)點(diǎn)

        imgView.origin=point;//更新當(dāng)前imgView 的位置

        //更新self.cutView frame
        self.cutView.height=self.bottomLeftImgView.y-self.topLeftImgView.y;
        self.cutView.width=self.cutView.height *_ratioWidth/_ratioHeight;
        
        //第4個(gè)點(diǎn)的位置
        self.bottomRigthImgView.x= imgView.x+self.cutView.width;
        self.bottomRigthImgView.y=imgView.y;
        
        //第1個(gè)點(diǎn)的位置
        self.topLeftImgView.x=imgView.x;
        
        //第2個(gè)點(diǎn)的位置
        self.topRigthImgView.x=self.bottomRigthImgView.x;
        self.topRigthImgView.y= self.topLeftImgView.y;
        
        self.cutView.origin=self.topLeftImgView.center;
        
        
    }else if (imgView ==self.bottomRigthImgView){
        //如果為第4個(gè)點(diǎn)
        
        imgView.origin=point;//更新當(dāng)前imgView 的位置

        //更新self.cutView frame
        self.cutView.height=imgView.y-self.topLeftImgView.y;
        self.cutView.width=self.cutView.height *_ratioWidth/_ratioHeight;

        
        //第3個(gè)點(diǎn)的位置
        self.bottomLeftImgView.x= imgView.x-self.cutView.width;
        self.bottomLeftImgView.y= imgView.y;
     
        //第1個(gè)點(diǎn)的位置
        self.topLeftImgView.x= self.bottomLeftImgView.x;
        
        //第2個(gè)點(diǎn)的位置
        self.topRigthImgView.x=imgView.x;
        self.topRigthImgView.y= self.topLeftImgView.y;
        
        self.cutView.origin=self.topLeftImgView.center;
    }else{
        return;
    }
    
    
}

-(UIImageView *)bgImgView{
    if (!_bgImgView) {
        _bgImgView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight-64)];
        _bgImgView.image=_image;
        _bgImgView.contentMode=UIViewContentModeScaleAspectFit;
        if (_bgImgView.image.size.width>=_bgImgView.image.size.height) {
            imageW=ScreenWidth;
            imageH=ScreenWidth *_bgImgView.image.size.height/_bgImgView.image.size.width;
        }else{
            imageH=ScreenHeight-64;
            imageW=imageH *_bgImgView.image.size.width/_bgImgView.image.size.height;
        }
        //計(jì)算圖片X,Y
         imageX=(ScreenWidth-imageW)/2;
         imageY=(ScreenHeight-64-imageH)/2 +64;
    }
    return _bgImgView;
}

-(UIView *)cutView{
    if (!_cutView) {
        _cutView=[[UIView alloc]initWithFrame:CGRectMake((ScreenWidth-_minWidth)/2, 0, _minWidth, _minWidth*_ratioHeight/_ratioWidth)];
        _cutView.centerY= self.bgImgView.centerY;
        _cutView.backgroundColor=[UIColor clearColor];
        [Methods setBorder:_cutView andFloat:1.0 andColor:RGB(35, 198, 167)];
    }
    return _cutView;
}

-(UIImageView *)topLeftImgView{
    if (!_topLeftImgView) {
        _topLeftImgView=[[UIImageView alloc]initWithFrame:CGRectMake(55, 80, imgViewWigth, imgViewWigth)];
//        _topLeftImgView.backgroundColor=RGB(35, 198, 167);
       _topLeftImgView.image= [Methods ImageFromColor:RGB(35, 198, 167) andCGRect:CGRectMake(0, 0, 10, 10)];
        _topLeftImgView.userInteractionEnabled=YES;
        _topLeftImgView.contentMode=UIViewContentModeCenter;
        UIPanGestureRecognizer *tap=[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleTDTap:)];
        tap.delegate = self;
        tap.cancelsTouchesInView = NO;
        tap.delaysTouchesEnded = NO;
        [self.topLeftImgView addGestureRecognizer:tap];
       
    }
    return _topLeftImgView;
}
-(UIImageView *)topRigthImgView{
    if (!_topRigthImgView) {
        _topRigthImgView=[[UIImageView alloc]initWithFrame:CGRectMake(55, 100, imgViewWigth, imgViewWigth)];
//        _topRigthImgView.backgroundColor=RGB(35, 198, 167);
        _topRigthImgView.image= [Methods ImageFromColor:RGB(35, 198, 167) andCGRect:CGRectMake(0, 0, 10, 10)];
        _topRigthImgView.contentMode=UIViewContentModeCenter;
        _topRigthImgView.userInteractionEnabled=YES;
        UIPanGestureRecognizer *tap=[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleTDTap:)];
        tap.delegate = self;
        tap.cancelsTouchesInView = NO;
        tap.delaysTouchesEnded = NO;
        [self.topRigthImgView addGestureRecognizer:tap];
       
    }
    return _topRigthImgView;
}
-(UIImageView *)bottomLeftImgView{
    if (!_bottomLeftImgView) {
        _bottomLeftImgView=[[UIImageView alloc]initWithFrame:CGRectMake(55, 120, imgViewWigth, imgViewWigth)];
//        _bottomLeftImgView.backgroundColor=RGB(35, 198, 167);
        _bottomLeftImgView.image= [Methods ImageFromColor:RGB(35, 198, 167) andCGRect:CGRectMake(0, 0, 10, 10)];
        _bottomLeftImgView.contentMode=UIViewContentModeCenter;
        _bottomLeftImgView.userInteractionEnabled=YES;
        UIPanGestureRecognizer *tap=[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleTDTap:)];
        tap.delegate = self;
        tap.cancelsTouchesInView = NO;
        tap.delaysTouchesEnded = NO;
        [self.bottomLeftImgView addGestureRecognizer:tap];
     
    }
    return _bottomLeftImgView;
}

-(UIImageView *)bottomRigthImgView{
    if (!_bottomRigthImgView) {
        _bottomRigthImgView=[[UIImageView alloc]initWithFrame:CGRectMake(55, 140, imgViewWigth, imgViewWigth)];
//        _bottomRigthImgView.backgroundColor=RGB(35, 198, 167);
        _bottomRigthImgView.image= [Methods ImageFromColor:RGB(35, 198, 167) andCGRect:CGRectMake(0, 0, 10, 10)];
        _bottomRigthImgView.contentMode=UIViewContentModeCenter;
        _bottomRigthImgView.userInteractionEnabled=YES;
        UIPanGestureRecognizer *tap=[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleTDTap:)];
        tap.delegate = self;
        tap.cancelsTouchesInView = NO;
        tap.delaysTouchesEnded = NO;
        [self.bottomRigthImgView addGestureRecognizer:tap];
   
    }
    return _bottomRigthImgView;
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    // 禁用 iOS7 返回手勢
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
//    self.navigationController.navigationBar.hidden=NO;
    
    
    // 開啟
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    }
}
@end

、、、

創(chuàng)建一個(gè)類,copy 你懂的,使用的時(shí)候需要添加PassImageDelegate這個(gè)代理方法,
然后在
、、、
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
//在這個(gè)代理方法里面我們做這樣的處理
  CutImgageVC *vc = [[CutImgageVC alloc] init];
    vc.delegate = self;
    vc.image = _picImage;//你獲取到的圖片
    vc.ratioWidth = 1.0;
    vc.ratioHeight = 1.0;
    vc.minWidth = 80.0;
    vc.view.backgroundColor = [UIColor whiteColor];
    picker.navigationBar.hidden = YES;
    [picker pushViewController:vc animated:YES];

}
//剪裁后回調(diào)的image
- (void)passImage:(UIImage *)image {
    _headImage.image = image;
    [self uploadimage];//這個(gè)是上傳圖片的方法,看你們怎么處理了
}

是不是很簡單啊,end。。。。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,012評論 25 709
  • 圖片處理中經(jīng)常用的圖片剪裁,就是通過剪裁框確定圖片剪裁的區(qū)域,然后剪去該區(qū)域的圖片,今天實(shí)現(xiàn)了一下,其實(shí)圖片剪裁本...
    jiangamh閱讀 782評論 0 5
  • 一、簡介: 大家應(yīng)該知道,我們在見面上顯示的ImageView的大小和從網(wǎng)絡(luò)取到的圖片的大小是不一樣的,最好的結(jié)果...
    萬戶猴閱讀 5,906評論 0 6
  • 這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)裁剪框和圖片剪裁功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下圖片處...
    木馬不在轉(zhuǎn)閱讀 11,609評論 9 10
  • 從事iOS開發(fā)已經(jīng)一年半有余了,發(fā)現(xiàn)整天去學(xué)這學(xué)那還不如靜下心來把會的學(xué)習(xí)牢靠。一些基礎(chǔ)的東西不好好學(xué)習(xí)會成...
    paraneaeee閱讀 772評論 0 1

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