一個裁剪圖片的小工具類,通過一句代碼調用,裁剪工具類

來源:互聯網
上載者:User

一個裁剪圖片的小工具類,通過一句代碼調用,裁剪工具類

前些時間空閑,寫了個簡單的小小工具類,即圖片的裁剪,動畫改變frame隱藏;

本類直接提供一個類方法調用,傳入3個參數即可,代碼非常簡單,誰都可以看懂;

參數說明:

第一個參數:你要將裁剪後的圖片添加到哪個視圖上執行動畫,傳入當前view即可;

第二個參數:傳入一張你要進行裁剪的圖片;

第三個參數:背景圖,可以添加一張背景(如預覽圖),不需要可以不傳,給一個空白字元串即可.如:@" ";

但不要傳 nil ,否則控制台會輸出如的莫名其妙的語句;

預覽圖效果:

 

以下是功能實現代碼:

- YYClipImageTool.h

 1 // 2 //  YYClipImageTool.h 3 //  YYClipImageDemo 4 // 5 //  Created by Arvin on 15/12/22. 6 //  Copyright © 2015年 Arvin. All rights reserved. 7 // 8  9 #import <UIKit/UIKit.h>10 11 @interface YYClipImageTool : UIImageView12 /**!13  *  @param view            要添加到的當前View14  *  @param image           要進行裁剪的圖片15  *  @param backgroundImage 可以設定背景圖片16  */17 + (void)addToCurrentView:(UIView *)view clipImage:(UIImage *)image backgroundImage:(NSString *)backgroundImage;18 19 @end

- YYClipImageTool.m

 1 // 2 //  YYClipImageTool.m 3 //  YYClipImageDemo 4 // 5 //  Created by Arvin on 15/12/22. 6 //  Copyright © 2015年 Arvin. All rights reserved. 7 // 8  9 #import "YYClipImageTool.h"10 11 #define Width view.frame.size.width12 #define Height view.frame.size.height13 #define imageW image.size.width14 #define imageH image.size.height * 0.515 #define duration 1.0f  // 動畫期間16 17 @interface YYClipImageTool ()18 19 @end20 21 @implementation YYClipImageTool22 23 + (void)addToCurrentView:(UIView *)view clipImage:(UIImage *)image backgroundImage:(NSString *)backgroundImage {24     25     // 上半部26     UIImageView *topImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, Width, Height * 0.5)];27     topImgView.image = [self clipImage:image withRect:CGRectMake(0, 0, imageW, imageH)];28     29     // 下半部30     UIImageView *bottomImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, Height * 0.5, Width, Height * 0.5)];31     bottomImgView.image = [self clipImage:image withRect:CGRectMake(0, imageH, imageW, imageH)];32     33     // 延時操作34     dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.5f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{35         // 執行動畫36         [UIView animateWithDuration:duration animations:^{37             CGRect topRect = topImgView.frame;38             topRect.origin.y -= imageH;39             topImgView.frame = topRect;40             41             CGRect bottomRect = bottomImgView.frame;42             bottomRect.origin.y += imageH;43             bottomImgView.frame = bottomRect;44         }];45     });46     47     // 背景圖48     UIImageView *bgImage = [[UIImageView alloc] initWithFrame:view.bounds];49     bgImage.image = [UIImage imageNamed:backgroundImage];50     51     // 添加到視圖52     [view addSubview:bgImage];53     [view addSubview:topImgView];54     [view addSubview:bottomImgView];55 }56 57 // 返回裁剪後的圖片58 + (UIImage *)clipImage:(UIImage *)image withRect:(CGRect)rect {59     CGRect clipFrame = rect;60     CGImageRef refImage = CGImageCreateWithImageInRect(image.CGImage, clipFrame);61     UIImage *newImage = [UIImage imageWithCGImage:refImage];62     CGImageRelease(refImage);63     return newImage;64 }65 66 /*67  // Only override drawRect: if you perform custom drawing.68  // An empty implementation adversely affects performance during animation.69  - (void)drawRect:(CGRect)rect {70  // Drawing code71  }72  */73 74 @end

本例在 viewController.m 中調用,代碼如下:

 1 // 2 //  ViewController.m 3 //  YYClipImageDemo 4 // 5 //  Created by Arvin on 15/12/22. 6 //  Copyright © 2015年 Arvin. All rights reserved. 7 // 8  9 #import "ViewController.h"10 #import "YYClipImageTool.h"11 12 @interface ViewController ()13 14 @end15 16 @implementation ViewController17 18 - (void)viewDidLoad {19     [super viewDidLoad];20     // Do any additional setup after loading the view, typically from a nib.21     22     UIImage *image = [UIImage imageNamed:@"Default_image"];23     [YYClipImageTool addToCurrentView:self.view clipImage:image backgroundImage:@"bgImage"];24 }25 26 - (void)didReceiveMemoryWarning {27     [super didReceiveMemoryWarning];28     // Dispose of any resources that can be recreated.29 }30 31 - (BOOL)prefersStatusBarHidden {32     return YES;33 }34 35 @end

END! 歡迎留言交流,一起學習...

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.