【问题标题】:Cropping UIImage in any image resolution [duplicate]以任何图像分辨率裁剪 UIImage [重复]
【发布时间】:2013-01-18 10:16:45
【问题描述】:

可能重复:
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

【问题讨论】:

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


【解决方案1】:

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

【讨论】:

  • 您可以利用上述库的优势编辑您的帖子吗?
  • @Odelya 我发送的链接中的库已经提供了一个示例项目。
  • 我看到了 calcFrameWithImage 方法 - 在我看来,这可能会有所帮助,但不确定具体如何。你参考它吗?也许您想发布相关代码?
【解决方案2】:
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();

【讨论】:

  • 这会调整它的大小,但不会裁剪它!如果我尝试设置 CGRectMake(x,y,itemSize.width,itemSize.height) 我会得到一个空白区域
猜你喜欢
  • 1970-01-01
  • 2016-06-27
  • 1970-01-01
  • 2013-01-15
  • 1970-01-01
  • 2013-05-20
  • 1970-01-01
  • 2013-01-14
  • 2023-04-02
相关资源
最近更新 更多