设为首页 加入收藏

TOP

一个裁剪图片的小工具类,通过一句代码调用(一)
2017-10-12 12:08:04 】 浏览:4786
Tags:一个 裁剪 图片 工具 通过 代码 调用

前些时间空闲,写了个简单的小小工具类,即图片的裁剪,动画改变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 : UIImageView
12 /**!
13  *  @param view            要添加到的当前View
14  *  @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.width
12 #define Height view.frame.size.height
13 #define imageW image.size.width
14 #define imageH image.size.height * 0.5
15 #define duration 1.0f  // 动画持续时间
16 
17 @interface YYClipImageTool ()
18 
19 @end
20 
21 @implementation YYClipImageTool
22 
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 code
71  }
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 reser
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇【原】SDWebImage源码阅读(一) 下一篇【原】SDWebImage源码阅读(一)

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目