当前位置:编程学习 > wap >>

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>


@inte易做图ce 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;
        //记录触摸点的所在位置
        if(mouseXY.x>sx+CONTROL_WIDTH&&mouseXY.x<ex-CONTROL_WIDTH&&mouseXY.y>sy+CONTROL_WIDTH&&mouseXY.y<ey-CONTROL_WIDTH){
            //NSLog(@"启动时已经进入");
            sxm = mouseXY.x-sx;
            sym = mouseXY.y-sy;
            number = 1;
        }else if(mouseXY.x>=ex-CONTROL_WIDTH && mouseXY.x<=ex+CONTROL_WIDTH && mouseXY.y>=ey-CONTROL_WIDTH && mouseXY.y<=ey+CONTROL_WIDTH){
            number = 2;
        }else if(mouseXY.x>=ex-CONTROL_WIDTH && mouseXY.x<=ex+CONTROL_WIDTH && mouseXY.y>=sy-CONTROL_WIDTH && mouseXY.y<=sy+CONTROL_WIDTH){
            number = 3;
        }else if(mouseXY.x>=sx-CONTROL_WIDTH && mouseXY.x<=sx+CONTROL_WIDTH && mouseXY.y>=ey-CONTROL_WIDTH && mouseXY.y<=ey+CONTROL_WIDTH){
            number = 4;
    &n

补充:移动开发 , IOS ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,