iphone图像裁剪功能实现

来源:互联网 发布:上海映速c4d教程淘宝 编辑:程序博客网 时间:2024/06/05 06:01

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

方案实现功能如下:

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

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

使用注意事项:

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


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

[plain] view plaincopy
  1. PictureCutView * pictureCutView;  
  2. pictureCutView.choosenImage; //获取选取区域部分图像对应的UIImage对象  

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

附注:

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

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

[plain] view plaincopy
  1. //  
  2. //  PictureCutView.h  
  3. //  Taonan  
  4. //  
  5. //  Created by zengconggen on 11-9-19.  
  6. //  Copyright 2011 yongmengsoft. All rights reserved.  
  7. //  
  8.   
  9. #import <UIKit/UIKit.h>  
  10.   
  11.   
  12. @interface PictureCutView : UIView {  
  13.   
  14.       
  15. @public  
  16.     UIImage * sourceImage;  
  17.     UIImage * choosenImage;  
  18.       
  19. @private  
  20.     UIImageView *bgImageView;       //要编辑的图片视图  
  21.     UIImageView *imageView;     //选择区域框的图片视图  
  22.     CGPoint mouseXY;                        //鼠标单击坐标  
  23.     CGFloat sx,sy,w,h,ex,ey,sxm,sym;  
  24.     /*  
  25.      sx:imageView的起始X坐标  
  26.      sy:imageView的起始y坐标  
  27.      w:imageView的width:宽  
  28.      h:imageView的height:高  
  29.      ex:imageView的右下角坐标endX:终止X坐标  
  30.      ey:imageView的endY:终止Y坐标  
  31.      sxm:触摸点距离imageView的起始X坐标的位置  
  32.      sym:触摸点距离imageView的起始Y坐标的位置  
  33.      */  
  34.     NSInteger number;                       //记录触摸点不同位置的不同处理方案  
  35.       
  36.     UIImage * originImage;  
  37.     CGFloat originSpace;  
  38.     CGFloat scale;  
  39.     CGFloat totalScale;  
  40.       
  41.     UITouch * currentTouch;  
  42. }  
  43.   
  44. @property(nonatomic,retain, setter=setSourceImage:) UIImage * sourceImage;  
  45. @property(nonatomic,readonly, getter=getChoosenImage) UIImage * choosenImage;  
  46. @property(nonatomic,retain) UIImageView *bgImageView;  
  47. @property(nonatomic,retain) UIImageView *imageView;  
  48. @property(nonatomic) CGPoint mouseXY;  
  49. @property(nonatomic) CGFloat sx,sy,w,h,ex,ey,sxm,sym;  
  50. @property(nonatomic) NSInteger number;  
  51.   
  52. @property(nonatomic,retain) UIImage * originImage;  
  53. @property(nonatomic) CGFloat scale;  
  54. @property(nonatomic) CGFloat totalScale;  
  55. @property(nonatomic) CGFloat originSpace;  
  56.   
  57. @property(nonatomic,retain) UITouch * currentTouch;  
  58.   
  59. - (void)setSourceImage:(UIImage *)image;  
  60. - (UIImage *)getChoosenImage;  
  61.   
  62. //裁剪图片  
  63. -(UIImage *)imageFromImage:(UIImage *)image inRect:(CGRect)rect;  
  64. //改变图片的大小  
  65. -(UIImage *)scaleFromImage:(UIImage *)image toSize:(CGSize)size;  
  66.   
  67. -(CGFloat)spaceToPoint:(CGPoint)first FromPoint:(CGPoint)two;  
  68. -(void)scaleTo:(CGFloat)x;  
  69. @end  

[plain] view plaincopy
  1. //  
  2. //  PictureCutView.m  
  3. //  Taonan  
  4. //  
  5. //  Created by zengconggen on 11-9-19.  
  6. //  Copyright 2011 yongmengsoft. All rights reserved.  
  7. //  
  8.   
  9. #import "PictureCutView.h"  
  10.   
  11.   
  12. #define CONTROL_WIDTH 20  
  13. #define MIN_OFFSET 5  
  14.   
  15. @implementation PictureCutView  
  16.   
  17.   
  18.   
  19. @synthesize sourceImage, choosenImage;  
  20. @synthesize bgImageView,imageView,mouseXY,sx,sy,w,h,ex,ey,sxm,sym,number,originImage,scale,totalScale,originSpace;  
  21.   
  22. @synthesize currentTouch;  
  23.   
  24. //触摸事件  
  25. -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event  
  26. {  
  27.     //NSLog(@"touchesBegan count:%d", [touches count]);  
  28.     [super touchesBegan:touches withEvent:event];  
  29.       
  30.     if ([touches count] ==2) {  
  31.         NSArray* twoTouches=[touches allObjects];  
  32.           
  33.         originSpace=[self spaceToPoint:[[twoTouches objectAtIndex:0] locationInView:self]  
  34.                              FromPoint:[[twoTouches objectAtIndex:1]locationInView:self]];  
  35.     }else if ([touches count] ==1 && currentTouch == nil) {  
  36.         imageView.alpha = 1.0;  
  37.         //CGRect bgRect = bgImageView.frame;  
  38.         //获取触摸点  
  39.         UITouch *touch = [touches anyObject];   
  40.         self.currentTouch = touch;  
  41.           
  42.         mouseXY = [touch locationInView:self];  
  43.         //NSLog(@"++++ mouseXY in touchesBegan(Touch:%@): (%f, %f)", touch,mouseXY.x, mouseXY.y);  
  44.           
  45.         //获取触摸时的各个参数  
  46.         sx = imageView.frame.origin.x;  
  47.         sy = imageView.frame.origin.y;  
  48.         w = imageView.frame.size.width;  
  49.         h = imageView.frame.size.height;  
  50.         ex = sx+w;  
  51.         ey = sy+h;  
  52.         //记录触摸点的所在位置  
  53.         if(mouseXY.x>sx+CONTROL_WIDTH&&mouseXY.x<ex-CONTROL_WIDTH&&mouseXY.y>sy+CONTROL_WIDTH&&mouseXY.y<ey-CONTROL_WIDTH){  
  54.             //NSLog(@"启动时已经进入");  
  55.             sxm = mouseXY.x-sx;   
  56.             sym = mouseXY.y-sy;  
  57.             number = 1;  
  58.         }else if(mouseXY.x>=ex-CONTROL_WIDTH && mouseXY.x<=ex+CONTROL_WIDTH && mouseXY.y>=ey-CONTROL_WIDTH && mouseXY.y<=ey+CONTROL_WIDTH){  
  59.             number = 2;  
  60.         }else if(mouseXY.x>=ex-CONTROL_WIDTH && mouseXY.x<=ex+CONTROL_WIDTH && mouseXY.y>=sy-CONTROL_WIDTH && mouseXY.y<=sy+CONTROL_WIDTH){  
  61.             number = 3;  
  62.         }else if(mouseXY.x>=sx-CONTROL_WIDTH && mouseXY.x<=sx+CONTROL_WIDTH && mouseXY.y>=ey-CONTROL_WIDTH && mouseXY.y<=ey+CONTROL_WIDTH){  
  63.             number = 4;  
  64.         }else if(mouseXY.x>=sx-CONTROL_WIDTH && mouseXY.x<=sx+CONTROL_WIDTH && mouseXY.y>=sy-CONTROL_WIDTH && mouseXY.y<=sy+CONTROL_WIDTH){  
  65.             number = 5;  
  66.         }else {  
  67.             number = 6;  
  68.         }  
  69.     }else {  
  70.         [super touchesBegan:touches withEvent:event];  
  71.     }  
  72. }  
  73.   
  74. //拖动事件  
  75. -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event  
  76. {  
  77.     NSAutoreleasePool * aPool = [[NSAutoreleasePool alloc] init];  
  78.       
  79.     //NSLog(@"touchesMoved count:%d", [touches count]);  
  80.     [super touchesMoved:touches withEvent:event];  
  81.     if ([touches count] ==2) {  
  82.         @synchronized(self)  
  83.         {  
  84.             NSArray* twoTouches=[touches allObjects];  
  85.               
  86.             CGFloat currSpace=[self spaceToPoint:[[twoTouches objectAtIndex:0] locationInView:self]  
  87.                                        FromPoint:[[twoTouches objectAtIndex:1]locationInView:self]];  
  88.               
  89.             //如果先触摸一根手指,再触摸另一根手指,则触发touchesMoved方法而不是touchesBegan方法  
  90.               
  91.             //此时originSpace应该是0,我们要正确设置它的值为当前检测到的距离,否则可能导致0除错误  
  92.               
  93.             if (originSpace==0) {  
  94.                 originSpace=currSpace;  
  95.             }  
  96.               
  97.             if (fabsf(currSpace-originSpace)>=MIN_OFFSET) {//两指间移动距离超过min_offset,识别为手势“捏合”  
  98.                 CGFloat s=currSpace/originSpace;//计算缩放比例  
  99.                 //NSLog(@"++++ scale: %f", s);  
  100.                   
  101.                 [self scaleTo:s];  
  102.                 originSpace = currSpace;  
  103.             }  
  104.         }  
  105.     }else if ([touches count] ==1) {  
  106.         //NSLog(@"Moveimageview:%@,poing的坐标:%@,sx:%f,sy:%f,w:%f,h:%f,ex:%f,ey:%f,sxm:%f,sym:%f,number:%i",imageView,NSStringFromCGPoint(mouseXY),sx,sy,w,h,ex,ey,sxm,sym,number);  
  107.         //NSLog(@"imageView的X坐标:%f,Y坐标:%f",imageView.frame.origin.x,imageView.frame.origin.y);  
  108.         UITouch *touch = [touches anyObject];  
  109.           
  110.         if ([touch isEqual:currentTouch]) {  
  111.             CGPoint point = [touch locationInView:self];  
  112.             CGRect bgRect = bgImageView.frame;  
  113.               
  114.             CGFloat x,y,width,height;  
  115.             if(number == 1){  
  116.                 //NSLog(@"修改前的边界:%@,X:%f,Y:%f,width:%f,height:%f",NSStringFromCGPoint(mouseXY),x,y,width,height);  
  117.                 //触摸点在imageview中  
  118.                 x = point.x-sxm;  
  119.                 y = point.y-sym;  
  120.                 width = w;  
  121.                 height = h;  
  122.             }else if (number == 2) {  
  123.                 //触摸点在imageview的右下角  
  124.                 x = sx;  
  125.                 y = sy;  
  126.                 width = point.x-sx;  
  127.                 height = point.y-sy;  
  128.             }else if(number == 3){  
  129.                 //触摸点在imageview的右上角  
  130.                 x = sx;  
  131.                 y = point.y;  
  132.                 if(point.y<sy){  
  133.                     height = h+sy-point.y;  
  134.                 }else {  
  135.                     height = ey-point.y;  
  136.                 }  
  137.                 if(point.x<ex){  
  138.                     width = w-(ex-point.x);  
  139.                 }else {  
  140.                     width = w+point.x-ex;  
  141.                 }  
  142.                   
  143.             }else if(number == 4){  
  144.                 //触摸点在imageview的左下角  
  145.                 x = point.x;  
  146.                 y = sy;  
  147.                 if(point.y<ey){  
  148.                     height = h-(ey-point.y);  
  149.                 }else {  
  150.                     height = h+point.y-ey;  
  151.                 }  
  152.                 width = ex-point.x;  
  153.                   
  154.             }else if(number == 5){  
  155.                 //触摸点在imageview的左上角  
  156.                 x = point.x;  
  157.                 y = point.y;  
  158.                 height = ey-point.y;  
  159.                 width = ex-point.x;  
  160.             }else {  
  161.                 //触摸点不在imageview上  
  162.                 //挪动整个视图  
  163.                 CGFloat offsetX = point.x-mouseXY.x;  
  164.                 CGFloat offsetY = point.y-mouseXY.y;  
  165.                   
  166.                 CGFloat bgSX = bgRect.origin.x+offsetX;  
  167.                 CGFloat bgSY = bgRect.origin.y+offsetY;  
  168.                 CGFloat bgEX = bgSX+bgRect.size.width;  
  169.                 CGFloat bgEY = bgSY+bgRect.size.height;  
  170.                   
  171.                 if (offsetX > 0) {  
  172.                     if (bgRect.size.width > self.frame.size.width) {  
  173.                         //判断左点是否入界,控制左边不显示空白  
  174.                         if (bgSX > 0) {  
  175.                             bgSX = 0;  
  176.                             offsetX = bgSX-bgRect.origin.x;  
  177.                         }  
  178.                     }else {  
  179.                         //控制右点是否出界,控制图片完整显示   
  180.                         if (bgEX > self.frame.size.width) {  
  181.                             bgEX = self.frame.size.width;  
  182.                             offsetX = bgEX - bgRect.origin.x-bgRect.size.width;  
  183.                         }  
  184.                     }  
  185.                 }else {  
  186.                     if (bgRect.size.width > self.frame.size.width) {  
  187.                         //判断右点是否入界,控制左边不显示空白  
  188.                         if (bgEX < self.frame.size.width) {  
  189.                             bgEX = self.frame.size.width;  
  190.                             offsetX = bgEX - bgRect.origin.x-bgRect.size.width;  
  191.                         }  
  192.                     }else {  
  193.                         //控制左点是否出界,控制图片完整显示   
  194.                         if (bgSX < 0) {  
  195.                             bgSX = 0;  
  196.                             offsetX = bgSX-bgRect.origin.x;  
  197.                         }  
  198.                     }  
  199.                 }  
  200.                   
  201.                 if (offsetY > 0) {  
  202.                     if (bgRect.size.height > self.frame.size.height) {  
  203.                         //判断上点是否入界,控制左边不显示空白  
  204.                         if (bgSY > 0) {  
  205.                             bgSY = 0;  
  206.                             offsetY = bgSY-bgRect.origin.y;  
  207.                         }  
  208.                     }else {  
  209.                         //控制下点是否出界,控制图片完整显示   
  210.                         if (bgEY > self.frame.size.height) {  
  211.                             bgEY = self.frame.size.height;  
  212.                             offsetY = bgEY - bgRect.origin.y-bgRect.size.height;  
  213.                         }  
  214.                     }  
  215.                 }else {  
  216.                     if (bgRect.size.height > self.frame.size.height) {  
  217.                         //判断下点是否入界,控制左边不显示空白  
  218.                         if (bgEY < self.frame.size.height) {  
  219.                             bgEY = self.frame.size.height;  
  220.                             offsetY = bgEY - bgRect.origin.y-bgRect.size.height;  
  221.                         }  
  222.                     }else {  
  223.                         //控制上点是否出界,控制图片完整显示   
  224.                         if (bgSY < 0) {  
  225.                             bgSY = 0;  
  226.                             offsetY = bgSY-bgRect.origin.y;  
  227.                         }  
  228.                     }  
  229.                 }  
  230.                   
  231.                 x = imageView.frame.origin.x+offsetX;  
  232.                 y = imageView.frame.origin.y+offsetY;  
  233.                 width = w;  
  234.                 height = h;  
  235.                   
  236.                 CGRect newBgRect = CGRectMake(bgRect.origin.x+offsetX, bgRect.origin.y+offsetY, bgRect.size.width, bgRect.size.height);  
  237.                   
  238.                 bgImageView.frame = newBgRect;  
  239.             }  
  240.             if(x-bgRect.origin.x<0){  
  241.                 width = imageView.frame.size.width;  
  242.                 x=bgRect.origin.x;  
  243.             }  
  244.             if(y-bgRect.origin.y<0){  
  245.                 height = imageView.frame.size.height;  
  246.                 y=bgRect.origin.y;  
  247.             }  
  248.             CGFloat xL,yL;  
  249.             xL = x+width;  
  250.             if(xL>bgRect.origin.x+bgRect.size.width){  
  251.                 x = bgRect.origin.x+bgRect.size.width-width;  
  252.             }  
  253.             yL = y+height;  
  254.             if(yL>bgRect.origin.y+bgRect.size.height){  
  255.                 y = bgRect.origin.y+bgRect.size.height-height;  
  256.             }  
  257.               
  258.             imageView.frame = CGRectMake(x, y, width, height);//容器大小的改变放在setimage前边,因为imageview的contentmode为scallapsecttofit模式  
  259.             if (number !=6) {  
  260.                 UIImage *endImage = [self imageFromImage:sourceImage inRect:CGRectMake(x-bgRect.origin.x, y-bgRect.origin.y, width, height)];  
  261.                 imageView.image = endImage;  
  262.             }  
  263.               
  264.             mouseXY = point;  
  265.             //NSLog(@"++++ mouseXY in touchesMoved(Touch:%@): (%f, %f)", touch,mouseXY.x, mouseXY.y);  
  266.         }  
  267.     }else {  
  268.         [super touchesMoved:touches withEvent:event];  
  269.     }  
  270.       
  271.     [aPool release];  
  272. }  
  273.   
  274. - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event  
  275. {  
  276.     //NSLog(@"touchesEnded count:%d", [touches count]);  
  277.     if ([[event allTouches] containsObject:currentTouch])  
  278.     {  
  279.         self.currentTouch = nil;  
  280.     }  
  281.       
  282.     self.originSpace = 0;  
  283.       
  284.     if ([touches count] ==2) {  
  285.         /*  
  286.         NSArray* twoTouches=[touches allObjects];  
  287.           
  288.         CGFloat currSpace=[self spaceToPoint:[[twoTouches objectAtIndex:0] locationInView:self]  
  289.                                    FromPoint:[[twoTouches objectAtIndex:1]locationInView:self]];  
  290.           
  291.         //如果先触摸一根手指,再触摸另一根手指,则触发touchesMoved方法而不是touchesBegan方法  
  292.           
  293.         //此时originSpace应该是0,我们要正确设置它的值为当前检测到的距离,否则可能导致0除错误  
  294.           
  295.         if (originSpace==0) {  
  296.             originSpace=currSpace;  
  297.         }  
  298.           
  299.         if (fabsf(currSpace-originSpace)>=MIN_OFFSET) {//两指间移动距离超过min_offset,识别为手势“捏合”  
  300.               
  301.             CGFloat s=currSpace/originSpace;//计算缩放比例  
  302.               
  303.             [self scaleTo:s];  
  304.               
  305.         }  
  306.          */  
  307.     }else {  
  308.         [super touchesEnded:touches withEvent:event];  
  309.     }  
  310.   
  311. }  
  312.   
  313. - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event  
  314. {  
  315.     //NSLog(@"touchesCanceled count:%d", [touches count]);  
  316.     if ([[event allTouches] containsObject:currentTouch])  
  317.     {  
  318.         self.currentTouch = nil;  
  319.     }  
  320.     self.originSpace = 0;  
  321.       
  322.     [super touchesCancelled:touches withEvent:event];  
  323. }  
  324.       
  325. -(void)scaleTo:(CGFloat)x{  
  326.       
  327.     scale=x;  
  328.     //totalScale *=scale;  
  329.       
  330.     //重设imageView的frame  
  331.     self.sourceImage = originImage;  
  332. }  
  333.   
  334.   
  335. - (id)initWithFrame:(CGRect)aframe {  
  336.     if (self = [super initWithFrame:aframe]) {  
  337.         self.contentMode = UIViewContentModeCenter;  
  338.         self.clipsToBounds = YES;  
  339.         self.exclusiveTouch = YES;  
  340.           
  341.         self.bgImageView = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, aframe.size.width, aframe.size.height)] autorelease];  
  342.         bgImageView.contentMode =UIViewContentModeScaleAspectFit;  
  343.         bgImageView.center = CGPointMake(aframe.size.width/2, aframe.size.height/2);  
  344.         bgImageView.alpha = 0.5;  
  345.         self.imageView = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)] autorelease];  
  346.         imageView.contentMode = UIViewContentModeScaleAspectFit;  
  347.         imageView.center = CGPointMake(aframe.size.width/2, aframe.size.height/2);  
  348.         [self addSubview:bgImageView];  
  349.         [self addSubview:imageView];  
  350.           
  351.         [self setUserInteractionEnabled:YES];  
  352.         [self setMultipleTouchEnabled:YES];  
  353.         [bgImageView setUserInteractionEnabled:NO];  
  354.         //[bgImageView setMultipleTouchEnabled:YES];  
  355.         [imageView setUserInteractionEnabled:NO];  
  356.         //[imageView setMultipleTouchEnabled:YES];  
  357.           
  358.         scale = 1.0f;  
  359.     }  
  360.     return self;  
  361. }  
  362.   
  363. - (id)init {  
  364.     return [self initWithFrame:CGRectMake(0, 44, 320, 416)];  
  365. }  
  366.   
  367. - (void)setSourceImage:(UIImage *)image  
  368. {  
  369.     NSAutoreleasePool * aPool = [[NSAutoreleasePool alloc] init];  
  370.       
  371.     BOOL firstDraw = YES;  
  372.       
  373.     if (self.originImage != nil) {  
  374.         firstDraw = NO;  
  375.     }else {  
  376.         self.originImage = image;  
  377.     }  
  378.   
  379.       
  380.     CGSize newSize = CGSizeMake(bgImageView.frame.size.width*scale, bgImageView.frame.size.height*scale);  
  381.       
  382.     CGFloat widthRate = newSize.width/originImage.size.width;  
  383.     CGFloat heightRate = newSize.height/originImage.size.height;  
  384.       
  385.     if (widthRate < 0.5 || heightRate < 0.5) {  
  386.         CGFloat lastTotalScale = totalScale;  
  387.         totalScale = 0.5;  
  388.         scale = totalScale/lastTotalScale;  
  389.         newSize = CGSizeMake(originImage.size.width*totalScale, originImage.size.height*totalScale);  
  390.     }else if (widthRate > 5 && heightRate > 5) {  
  391.         CGFloat lastTotalScale = totalScale;  
  392.         totalScale = 5;  
  393.         scale = totalScale/lastTotalScale;  
  394.         newSize = CGSizeMake(originImage.size.width*totalScale, originImage.size.height*totalScale);  
  395.     }  
  396.   
  397.     sourceImage = [[self scaleFromImage:image toSize:newSize] retain];  
  398.     totalScale = sourceImage.size.width/originImage.size.width;  
  399.     newSize = sourceImage.size;  
  400.       
  401.     CGRect newBgRect;  
  402.     if (widthRate != heightRate) {  
  403.         //适应bgimageview大小  
  404.         newBgRect = CGRectMake((self.frame.size.width*scale-newSize.width)/2, (self.frame.size.height*scale-newSize.height)/2, newSize.width, newSize.height);  
  405.     }else {  
  406.         newBgRect = CGRectMake(bgImageView.frame.origin.x*scale, bgImageView.frame.origin.y*scale, newSize.width, newSize.height);  
  407.     }  
  408.       
  409.     bgImageView.frame = newBgRect;  
  410.     CGRect newChooseRect = CGRectMake(imageView.frame.origin.x*scale, imageView.frame.origin.y*scale, imageView.frame.size.width*scale, imageView.frame.size.height*scale);  
  411.     imageView.frame = newChooseRect;  
  412.       
  413.     if (firstDraw) {  
  414.         bgImageView.image = sourceImage;  
  415.     }  
  416.     sx = (imageView.frame.origin.x > newBgRect.origin.x)? imageView.frame.origin.x : newBgRect.origin.x;  
  417.     sy = (imageView.frame.origin.y > newBgRect.origin.y)? imageView.frame.origin.y : newBgRect.origin.y;  
  418.     w = imageView.frame.size.width;  
  419.     h = imageView.frame.size.height;  
  420.     ex = sx+w;  
  421.     ey = sy+h;  
  422.     if (ex > newBgRect.origin.x+newBgRect.size.width) {  
  423.         sx = newBgRect.origin.x+newBgRect.size.width - w;  
  424.     }  
  425.     if (ey > newBgRect.origin.y+newBgRect.size.height) {  
  426.         sy = newBgRect.origin.x+newBgRect.size.height - h;  
  427.     }  
  428.       
  429.     UIImage *endImage = [self imageFromImage:sourceImage inRect:CGRectMake(sx-newBgRect.origin.x, sy-newBgRect.origin.y, w, h)];  
  430.     imageView.image = endImage;  
  431.       
  432.     [aPool release];  
  433. }  
  434. - (UIImage *)getChoosenImage  
  435. {  
  436.     return imageView.image;  
  437. }  
  438.   
  439. -(UIImage *)imageFromImage:(UIImage *)image inRect:(CGRect)rect{  
  440.     //CGRect realRect = CGRectMake(rect.origin.x/totalScale , rect.origin.y/totalScale, rect.size.width/totalScale, rect.size.height/totalScale);  
  441.     CGRect realRect = rect;  
  442.     CGImageRef sourceImageRef = [image CGImage];  
  443.     CGImageRef newImageRef = CGImageCreateWithImageInRect(sourceImageRef, realRect);   
  444.     UIImage *newImage = [[[UIImage alloc] initWithCGImage:newImageRef] autorelease];  
  445.     CGImageRelease(newImageRef);  
  446.       
  447.     return newImage;        
  448. }    
  449.   
  450.   
  451. -(UIImage *)scaleFromImage:(UIImage *)image toSize:(CGSize)size  
  452. {  
  453.     CGFloat imageRate = image.size.width/image.size.height;  
  454.     CGFloat newRate = size.width/size.height;  
  455.     if (imageRate > newRate) {  
  456.         size.height = image.size.height * size.width/image.size.width;  
  457.     }else {  
  458.         size.width = image.size.width * size.height/image.size.height;  
  459.     }  
  460.     //scale = size.width/image.size.width * scale;  
  461.       
  462.     UIGraphicsBeginImageContext(size);  
  463.     [image drawInRect:CGRectMake(0, 0, size.width, size.height)];  
  464.     UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();  
  465.     UIGraphicsEndImageContext();  
  466.       
  467.     //CGSize imgSize = newImage.size;  
  468.       
  469.     return newImage;  
  470. }  
  471.   
  472. -(CGFloat)spaceToPoint:(CGPoint)first FromPoint:(CGPoint)two{//计算两点之间的距离  
  473.       
  474.     float x = first.x - two.x;  
  475.       
  476.     float y = first.y - two.y;  
  477.       
  478.     return sqrt(x * x + y * y);  
  479.       
  480. }  
  481.   
  482. - (void)dealloc {  
  483.     [bgImageView removeFromSuperview];  
  484.     [imageView removeFromSuperview];  
  485.       
  486.     [sourceImage release];  
  487.     [bgImageView release];  
  488.     [imageView release];  
  489.       
  490.     [originImage release];  
  491.     [currentTouch release];  
  492.       
  493.     [super dealloc];  
  494. }  
  495.   
  496.   
  497.   
  498.   
  499. @end 
0 0