IOS 截取任意图片

8 篇文章 0 订阅
4 篇文章 0 订阅
#import <UIKit/UIKit.h>

//定义协议,方便扩展
@protocol ClipViewDelegate <NSObject>

@optional
-(void) drawImageRect:(CGRect) rect;

@end

@interface ClipView : UIView
{
    CGPoint _startPoint;    //手指起点
    CGPoint _endPoint;      //手指终点
    CGRect _targetImageViewFrame;   //手指划出的区域
}
@property(nonatomic,assign)id<ClipViewDelegate>delegate;        //委托
@end
#import "ClipView.h"

@implementation ClipView
@synthesize delegate = _delegate;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) 
    {
        // Initialization code
        _targetImageViewFrame = CGRectZero;
    }
    return self;
}


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();//获取上下文
	CGContextSetRGBStrokeColor(context, 0, 0, 1,1); //使用RGBA设置颜色
	CGContextSetLineWidth(context, 1);              //设置线条粗细
	CGFloat f[2] = {10.0,10.0};
	CGContextSetLineDash(context, 2, f, 2); //设置虚线样式,虚线样式为   --  --  --  --
	CGContextStrokeRect(context, _targetImageViewFrame);//绘制矩形

}


//开始的时候记录起始坐标
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    _startPoint = [touch locationInView:self];
}
//移动过程中记录终点,并不断生成矩形,绘制矩形
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    _endPoint = [touch locationInView:self];
    
    _targetImageViewFrame.origin.x = _startPoint.x;
    _targetImageViewFrame.origin.y = _startPoint.y;
    
    _targetImageViewFrame.size.width = _endPoint.x - _startPoint.x;
    _targetImageViewFrame.size.height = _endPoint.y - _startPoint.y;
    
    [self setNeedsDisplay];
}

//结束之后进行截图处理
-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    if(_delegate && [_delegate respondsToSelector:@selector(drawImageRect:)])
    {
        [_delegate drawImageRect:_targetImageViewFrame];
    }
}

@end
在代理文件中 加入

@interface AppDelegate : UIResponder <UIApplicationDelegate,ClipViewDelegate>通过

UIImageView获取截取的图片

ClipView *clipView = [[ClipView alloc] initWithFrame:self.window.frame];
    clipView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"iphone.jpg"]];
    clipView.delegate = self;
    [self.window addSubview:clipView];
    [clipView release];
    
    _imageView = [[UIImageView alloc] initWithFrame:CGRectZero];
    [self.window addSubview:_imageView];

-(void) drawImageRect:(CGRect) rect
{
    UIImage *image = [UIImage imageNamed:@"iphone.jpg"];//原图
    CGImageRef imageRef = image.CGImage;
    CGImageRef subImageRef = CGImageCreateWithImageInRect(imageRef, rect);//原图中抠出来部分图
    UIImage *subImage = [UIImage imageWithCGImage:subImageRef];//新图
    _imageView.image = subImage;
    _imageView.frame =  rect;
    
    [UIView beginAnimations:nil context:nil];//改变frame,改变过程中添加动画
    [UIView setAnimationDuration:0.5];
    
    _imageView.frame = CGRectMake(0, 20, fabsf(rect.size.width), fabsf(rect.size.height));
    
    [UIView commitAnimations];
    
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值