以任何图像分辨率裁剪UIImage

时间:2013-02-03 16:12:45

标签: iphone objective-c cocoa-touch uiimage cgimage

  

可能重复:
  Cropping a UIImage

我看过许多关于裁剪图像的教程,我正在尝试运气,但没有成功。

我需要从AVFoundation裁剪图像。由于从相机拍摄图像时,它从纵向模式向左旋转,我还需要向右旋转,我的x和y相反。问题是,如果我将图像的帧发送到我要驻留的位置,在我看来,图像和矩形的大小没有相关性。

代码是:

@property (weak, nonatomic) IBOutlet UIView *videoPreviewView;
...
...
int width = videoPreviewView.frame.size.width;
int height = videoPreviewView.frame.size.height;
int x = videoPreviewView.frame.origin.x;
int y = videoPreviewView.frame.origin.y;
CGRect croprect = CGRectMake(y, x,height,width);
// Draw new image in current graphics context
CGImageRef imageRef = CGImageCreateWithImageInRect([sourceImage CGImage], croprect);
// Create new cropped UIImage
UIImage *resultImage = [UIImage imageWithCGImage:imageRef scale:[sourceImage scale] orientation:UIImageOrientationRight];

当我打印框架的尺寸时,我得到:

(4.5,69.5,310,310)

,图片尺寸为:

(720,1280)

如何在任何图像分辨率下执行裁剪?

我尝试将值乘以image.scale - 然而,值为1.00

2 个答案:

答案 0 :(得分:1)

尝试这个。这肯定会帮助你。 https://github.com/barrettj/BJImageCropper

答案 1 :(得分:0)

To resize image, Try this    

    UIImage *image = [UIImage imageNamed:@"image.png"];

    CGSize itemSize = CGSizeMake((your Width), (your Height));
    UIGraphicsBeginImageContext(itemSize);
    CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
    [image drawInRect:imageRect];

    UIImage * yourCroppedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

To Crop,

    // Create new image context
    UIGraphicsBeginImageContext(SIZE);

    // Create CGRect for image
    CGRect newRect = CGRectMake(x, y,SIZE.width,SIZE.height);

    // Draw the image into the rect
    [ORIGINALIMAGE drawInRect:newRect];

    // Saving the image, ending image context
    UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();