【问题标题】:Image cropping issue in iOSiOS中的图像裁剪问题
【发布时间】:2012-12-12 06:34:11
【问题描述】:

我正在创建一个具有图像裁剪功能的 iPhone 应用程序。在此,我从 UIImagePickerController 获取照片并将其传递给裁剪。它有一个滚动视图,所选图像将作为子视图添加到滚动视图。我正在使用 UIButton 来选择裁剪区域。用户可以将按钮移动到 imageview 上并放置在任何地方,当点击 CROP 按钮时,应该从 imageview 中裁剪出与按钮框架大小相似的区域。

我使用了以下代码,但它没有返回实际图像。

CGRect clippedRect  = CGRectMake(self.scrollView.frame.origin.x+90, self.scrollView.frame.origin.y, self.scrollView.frame.size.width-180, self.scrollView.frame.size.height-220);
CGImageRef imageRef = CGImageCreateWithImageInRect([self.myPhoto CGImage], clippedRect);
UIImage *newImage   = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);

self.imageView.image = newImage;

也用过

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

但结果图像与按钮框架不同。我正在从另一个区域获取图像。

更新代码

- (void)loadPhoto{

    CGFloat w = self.myPhoto.size.width;
    CGFloat h = self.myPhoto.size.height;
    CGRect imageViewFrame = CGRectMake(0.0f, 0.0f, roundf(w / 2.0f), roundf(h / 2.0f));
    self.scrollView.contentSize = imageViewFrame.size;

    UIImageView *iv = [[UIImageView alloc] initWithFrame:imageViewFrame];
    iv.contentMode = UIViewContentModeScaleAspectFit;
    iv.image = self.myPhoto;
    [self.view addSubview:iv];
    self.imageView = iv;
    [iv release];    
}

CGRect crop;//= CGRectMake(10, 10, 360, 360);
crop.origin.x = self.cropFrame.frame.origin.x;
crop.origin.y = self.cropFrame.frame.origin.y;
crop.size.width = roundf(self.cropFrame.frame.size.width * 2.0f); //self.cropFrame.frame.size.width * 2;
crop.size.height = roundf(self.cropFrame.frame.size.height * 2.0f);  //self.cropFrame.frame.size.height * 2;

NSLog(@"Rect: %@", NSStringFromCGRect(crop));
self.imageView.image = [self croppedImage:crop];

- (UIImage *)croppedImage:(CGRect)bounds {
    CGImageRef imageRef = CGImageCreateWithImageInRect([self.imageView.image CGImage], bounds);
    UIImage *croppedImage = [UIImage imageWithCGImage:imageRef scale:1.0 orientation:self.myPhoto.imageOrientation];
    CGImageRelease(imageRef);
    return croppedImage;
}

请帮忙寻找解决办法。

【问题讨论】:

  • 查看this UIImage category,应该可以正常工作。
  • 感谢您提供该代码,但仍然无法正常工作,我已使用最新代码更新了该问题。我尝试避免滚动视图,但仍然没有帮助。
  • “它没有返回实际图像”是什么意思?
  • 我可以移动一个按钮框架来设置实际图像的位置。现在我可以通过该按钮框架看到一个区域。但是当我点击裁剪时,结果图像并不完全是通过按钮框架出现的图像。它的位置和大小正在发生变化。
  • 我想我快到了,但我还没有找到问题所在。如果有人可以帮忙,请。

标签: ios image uiscrollview imageview crop


【解决方案1】:

相信你已经解决了这个问题。我在尝试裁剪功能时也遇到了这个问题

将 image.size 设置为 imageView.size 和 scrollView.contentSize。下面的代码将给矩形裁剪

cropRect.origin = scrollView.contentOffset;
cropRect.size = scrollView.bounds.size;    
cropRect.origin.x /= scrollView.zoomScale;
cropRect.origin.y /= scrollView.zoomScale;
cropRect.size.width /= scrollView.zoomScale;
cropRect.size.height /= scrollView.zoomScale;

如果计划首先在可见矩形上显示完整图像。将 imageView.size 和 scrollView.contentSize 设置为可见视图大小将给出其他区域的裁剪图像。而是尝试通过

找到缩放比例
CGFloat dxWidth = viewCrop.frame.size.width / imageView.image.size.width;
CGFloat dxHeight = viewCrop.frame.size.height / imageView.image.size.height;
CGFloat zoomScale = fmaxf(dWidth, dHeight)

并应用(如果通过添加 subView 然后在 addSubView 之后)

scrollView.minimumZoomScale = zoomScale; // to disable further zoom-out
[scrollView setZoomScale: zoomScale];

【讨论】:

    【解决方案2】:

    你可以使用我制作的这个裁剪工具。它本质上为您提供了一个界面,允许用户选择裁剪区域。我认为它符合您的要求。

    https://github.com/nicholjs/BFCropInterface

    【讨论】:

    • 你可以在单个图像视图中添加两个裁剪吗?
    【解决方案3】:

    由于您使用的是允许滚动图像的 scrollView,因此您需要将裁剪矩形调整到 scrollView 的位置:

    float zoomScale = self.scrollView.zoomScale;
    int cropX = (self.scrollView.contentOffset.x-imageView.frame.origin.x)/zoomScale;
    int cropY = (self.scrollView.contentOffset.y-imageView.frame.origin.y)/zoomScale;
    

    【讨论】:

      【解决方案4】:

      iOS 有一个裁剪图像的默认功能。试试这个代码。

          picker.allowsEditing = YES;
      

      并检查此控制器是否裁剪..这正是您正在寻找的那个我认为https://github.com/barrettj/BJImageCropper。希望这对您有所帮助..

      【讨论】:

        猜你喜欢
        • 2017-05-22
        • 2013-03-05
        • 2014-04-05
        • 2015-09-30
        • 1970-01-01
        • 1970-01-01
        • 2019-10-23
        • 2016-06-16
        • 2011-10-28
        相关资源
        最近更新 更多