【发布时间】: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