2

我正在创建一个具有图像裁剪功能的 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;
}

请帮助找到解决方案。

4

4 回答 4

0

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

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;
于 2012-12-27T10:42:53.797 回答
0

你可以使用我制作的这个裁剪工具。它本质上为您提供了一个界面,允许用户选择裁剪区域。我认为这与您正在寻找的一致。

https://github.com/nicholjs/BFCropInterface

于 2013-02-28T19:45:52.613 回答
0

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

    picker.allowsEditing = YES;

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

于 2012-12-27T07:40:46.707 回答
0

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

将 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];
于 2017-06-02T15:46:11.683 回答