问题: 裁剪图像缩小很好。 使用放大的图像进行裁剪会将图像显示在应该是的上方。 我在那里的yOffset是因为我想要的裁剪方块在scrollview所在的下方开始。
代码:
CGRect rect;
float yOffset = 84;
rect.origin.x = floorf([scrollView contentOffset].x * zoomScale);
rect.origin.y = floorf(([scrollView contentOffset].y + yOffset) * zoomScale);
rect.size.width = floorf([scrollView bounds].size.width * zoomScale);
rect.size.height = floorf((320 * zoomScale));
if (rect.size.width > 320) {
rect.size.width = 320;
}
if (rect.size.height > 320) {
rect.size.height = 320;
}
CGImageRef cr = CGImageCreateWithImageInRect([[imageView image] CGImage], rect);
UIImage *img = imageView.image; //[UIImage imageWithCGImage:cr];
UIGraphicsBeginImageContext(rect.size);
// translated rectangle for drawing sub image
CGRect drawRect = CGRectMake(-rect.origin.x, -rect.origin.y, 320.0f, 320.0f);
NSLog(@"drawRect: %@", NSStringFromCGRect(drawRect));
NSLog(@"rect: %@", NSStringFromCGRect(rect));
// draw image
[img drawInRect:drawRect];
// grab image
UIImage *cropped = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGImageRelease(cr);
[self.delegate imageCropper:self didFinishCroppingWithImage:cropped];
我在做什么导致图像在缩放时得到错误的高度?
答案 0 :(得分:11)
UIImage* imageFromView(UIImage* srcImage, CGRect* rect)
{
CGImageRef cr = CGImageCreateWithImageInRect(srcImage.CGImage, *rect);
UIImage* cropped = [UIImage imageWithCGImage:cr];
CGImageRelease(cr);
return cropped;
}
-(void) doneEditing
{
//Calculate the required area from the scrollview
CGRect visibleRect;
float scale = 1.0f/scrollView.zoomScale;
visibleRect.origin.x = scrollView.contentOffset.x * scale;
visibleRect.origin.y = scrollView.contentOffset.y * scale;
visibleRect.size.width = scrollView.bounds.size.width * scale;
visibleRect.size.height = scrollView.bounds.size.height * scale;
FinalOutputView* outputView = [[FinalOutputView alloc] initWithNibName:@"FinalOutputView" bundle:[NSBundle mainBundle]];
outputView.image = imageFromView(imageView.image, &visibleRect);
[self.navigationController pushViewController:outputView animated:YES];
[outputView release];
}
加载原始图片:
缩放图片:
最后捕捉图片
答案 1 :(得分:0)
如果你想从scrollView的整个视图中获取屏幕截图(缩放后),你可以这样做:
UIImage* image = nil;
UIGraphicsBeginImageContext(self.scrollView.contentSize);
{
//save previous frames
CGPoint savedContentOffset = self.scrollView.contentOffset;
CGRect savedFrame = self.scrollView.frame;
CGRect imgFrame = self.imageView.frame;
//set the frames with current content size
self.scrollView.contentOffset = CGPointZero;
self.scrollView.frame = CGRectMake(0, 0, self.scrollView.contentSize.width, self.scrollView.contentSize.height);
self.imageView.frame = self.scrollView.frame;
//render image now :)
[self.scrollView.layer renderInContext: UIGraphicsGetCurrentContext()];
image = UIGraphicsGetImageFromCurrentImageContext();
//now set the frames again with old ones :)
self.scrollView.contentOffset = savedContentOffset;
self.scrollView.frame = savedFrame;
self.imageView.frame = imgFrame;
[self viewForZoomingInScrollView:self.scrollView];
}
UIGraphicsEndImageContext();
//get the documents path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//save file as savedImage.png
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:@"savedImage.png"];
//get the png data from image
NSData *imageData = UIImagePNGRepresentation(image);
//write it now
[imageData writeToFile:savedImagePath atomically:NO];