iphone图像裁剪功能实现

这两天在做图像剪裁功能。一致在尝试不同的解决方案,包括从cocoachina查找的资料创意,一直不满意最终的效果。经过2天努力,终于完美实现。

方案实现功能如下:

1、可拖拽、缩放选区,截取所选区域部分图像

2、可缩放被裁剪图像,移动被裁剪图像,方便用户精确裁剪。

使用注意事项:

1、不要将代码实现的视图类实例添加为UIScrollView类实例的子视图。因为UIScrollView类实例会屏蔽子视图的拖拽事件(除非您自己实现一个子类,继承UIScrollView类,并按照苹果官方指南重写指定的几个方法。个人认为比较麻烦,而且不方便)。


2、若要获取选区对应的区域部分图像。使用

PictureCutView * pictureCutView;
pictureCutView.choosenImage; //获取选取区域部分图像对应的UIImage对象

 

方案已尽我最大努力实现优化,如果您有更好的优化意见,欢迎留言提出。

附注:

1、本代码部分参考网上资料,部分代码来源与网上。

2、本人保留代码的版权,如需使用代码,请保留版权说明。

//
//  PictureCutView.h
//  Taonan
//
//  Created by zengconggen on 11-9-19.
//  Copyright 2011 yongmengsoft. All rights reserved.
//

#import <UIKit/UIKit.h>


@interface PictureCutView : UIView {

    
@public
    UIImage * sourceImage;
    UIImage * choosenImage;
    
@private
	UIImageView *bgImageView;		//要编辑的图片视图
	UIImageView *imageView;		//选择区域框的图片视图
	CGPoint mouseXY;						//鼠标单击坐标
	CGFloat sx,sy,w,h,ex,ey,sxm,sym;
	/*
	 sx:imageView的起始X坐标
	 sy:imageView的起始y坐标
	 w:imageView的width:宽
	 h:imageView的height:高
	 ex:imageView的右下角坐标endX:终止X坐标
	 ey:imageView的endY:终止Y坐标
	 sxm:触摸点距离imageView的起始X坐标的位置
	 sym:触摸点距离imageView的起始Y坐标的位置
	 */
	NSInteger number;						//记录触摸点不同位置的不同处理方案
    
    UIImage * originImage;
    CGFloat originSpace;
    CGFloat scale;
    CGFloat totalScale;
    
    UITouch * currentTouch;
}

@property(nonatomic,retain, setter=setSourceImage:) UIImage * sourceImage;
@property(nonatomic,readonly, getter=getChoosenImage) UIImage * choosenImage;
@property(nonatomic,retain) UIImageView *bgImageView;
@property(nonatomic,retain) UIImageView *imageView;
@property(nonatomic) CGPoint mouseXY;
@property(nonatomic) CGFloat sx,sy,w,h,ex,ey,sxm,sym;
@property(nonatomic) NSInteger number;

@property(nonatomic,retain) UIImage * originImage;
@property(nonatomic) CGFloat scale;
@property(nonatomic) CGFloat totalScale;
@property(nonatomic) CGFloat originSpace;

@property(nonatomic,retain) UITouch * currentTouch;

- (void)setSourceImage:(UIImage *)image;
- (UIImage *)getChoosenImage;

//裁剪图片
-(UIImage *)imageFromImage:(UIImage *)image inRect:(CGRect)rect;
//改变图片的大小
-(UIImage *)scaleFromImage:(UIImage *)image toSize:(CGSize)size;

-(CGFloat)spaceToPoint:(CGPoint)first FromPoint:(CGPoint)two;
-(void)scaleTo:(CGFloat)x;
@end

//
//  PictureCutView.m
//  Taonan
//
//  Created by zengconggen on 11-9-19.
//  Copyright 2011 yongmengsoft. All rights reserved.
//

#import "PictureCutView.h"


#define CONTROL_WIDTH 20
#define MIN_OFFSET 5

@implementation PictureCutView



@synthesize sourceImage, choosenImage;
@synthesize bgImageView,imageView,mouseXY,sx,sy,w,h,ex,ey,sxm,sym,number,originImage,scale,totalScale,originSpace;

@synthesize currentTouch;

//触摸事件
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //NSLog(@"touchesBegan count:%d", [touches count]);
    [super touchesBegan:touches withEvent:event];
    
	if ([touches count] ==2) {
        NSArray* twoTouches=[touches allObjects];
        
        originSpace=[self spaceToPoint:[[twoTouches objectAtIndex:0] locationInView:self]
                             FromPoint:[[twoTouches objectAtIndex:1]locationInView:self]];
    }else if ([touches count] ==1 && currentTouch == nil) {
        imageView.alpha = 1.0;
        //CGRect bgRect = bgImageView.frame;
        //获取触摸点
        UITouch *touch = [touches anyObject]; 
        self.currentTouch = touch;
        
        mouseXY = [touch locationInView:self];
        //NSLog(@"++++ mouseXY in touchesBegan(Touch:%@): (%f, %f)", touch,mouseXY.x, mouseXY.y);
        
        //获取触摸时的各个参数
        sx = imageView.frame.origin.x;
        sy = imageView.frame.origin.y;
        w = imageView.frame.size.width;
        h = imageView.frame.size.height;
        ex = sx+w;
        ey = sy+h;
        //记录触摸点的所在位置
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值