2013-03-16 68 views
1

我要裁剪的UIImage用下面的代码:图像修剪问题

- (UIImage*)imageByCropping:(UIImage *)imageToCrop toRect:(CGRect)rect 
{ 
    CGImageRef imageRef = CGImageCreateWithImageInRect([imageToCrop CGImage], rect); 
    // or use the UIImage wherever you like 
    UIImage * img = [UIImage imageWithCGImage:imageRef]; 
    CGImageRelease(imageRef); 
    return img; 
} 

此代码工作在模拟器不错,但给人的设备不寻常的结果。

+1

描述 “设备上不寻常的结果” 进一步... – henser 2013-03-16 12:52:16

+0

检查图像的'scale',上视网膜设备,您可能需要相应地调整矩形的坐标。 – omz 2013-03-16 12:53:36

+0

谢谢@henser但下面的答案工作我foe。 其实裁剪区域图像比裁剪更大。 使用像男人这样的词是不允许的? – 2013-03-16 13:43:11

回答

0

创建一个UIImage的类别,然后尝试在此。

@implementation UIImage的(作物)

- (UIImage *)crop:(CGRect)cropRect { 

    cropRect = CGRectMake(cropRect.origin.x*self.scale, 
         cropRect.origin.y*self.scale, 
         cropRect.size.width*self.scale, 
         cropRect.size.height*self.scale);  

    CGImageRef imageRef = CGImageCreateWithImageInRect([self CGImage], cropRect); 
    UIImage *result = [UIImage imageWithCGImage:imageRef 
              scale:self.scale 
            orientation:self.imageOrientation]; 
    CGImageRelease(imageRef); 
    return result; 
} 
0

试试这个:

- (UIImage *)cropImage:(UIImage *)oldImage { 
      CGSize imageSize = oldImage.size; 
      UIGraphicsBeginImageContextWithOptions(CGSizeMake(imageSize.width,imageSize.height - 150),NO,0.); 
      [oldImage drawAtPoint:CGPointMake(0, -80) blendMode:kCGBlendModeCopy alpha:1.]; 
      UIImage *croppedImage = UIGraphicsGetImageFromCurrentImageContext(); 
      UIGraphicsEndImageContext(); 
      return croppedImage; 
    } 
+0

你可以设置高度,宽度,x和y。 – 2013-03-16 13:41:37

+0

为什么你减少了150 – 2013-03-16 13:43:54

+0

的大小,因为我采取了屏幕截图,并且我在底部有tabbar。 – 2013-03-16 13:47:39