A small tool class for image cropping. It is called using a code to crop the tool class.

Source: Internet
Author: User

A small tool class for image cropping. It is called using a code to crop the tool class.

Some time ago, I wrote a simple small tool class, that is, image cropping, and animation changes to frame hiding;

This class directly provides a class method call, which can be passed in with three parameters. The code is very simple and can be understood by anyone;

Parameter description:

The first parameter: Specifies the view on which the cropped image is added for animation and the current view;

The second parameter: input an image you want to crop;

The third parameter is the background image. You can add a background (such as a preview image) without passing the background image. For example :@"";

But do not upload nil; otherwise, the console will output an inexplicable statement;

Preview the image effect:

 

The following is the function implementation code:

-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 the current View14 * @ param image the image to be cropped 15 * @ param backgroundImage you can set the background image 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 // animation duration 16 17 @ interface YYClipImageTool () 18 19 @ end20 21 @ implementation YYClipImageTool22 23 + (void) addToCurrentView :( UIView *) view clipImage :( UIImage *) image backgroundImage :( NSString *) backgroundImage {24 25 // upper half 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 // lower 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 // delayed operation 34 dispatch_after (dispatch_time (DISPATCH_TIME_NOW, (int64_t) (2.5f * NSEC_PER_SEC), dispatch_get_main_queue (), ^ {35 // run the animation 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 // background image 48 UIImageView * bgImage = [[UIImageView alloc] initWithFrame: view. bounds]; 49 bgImage. image = [UIImage imageNamed: backgroundImage]; 50 51 // Add to view 52 [view addSubview: bgImage]; 53 [view addSubview: topImgView]; 54 [view addSubview: bottomImgView]; 55} 56 57 // return the cropped image 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

This example is called in viewController. m. The Code is as follows:

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 didreceivemorywarning]; 28 // Dispose of any resources that can be recreated.29} 30 31-(BOOL) prefersStatusBarHidden {32 return YES; 33} 34 35 @ end

END! You are welcome to leave a message and learn together...

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.