0

问题:缩小图像进行裁剪很好。使用放大的图像进行裁剪显示图像高于应有的图像。我在那里的 yOffset 是因为我想要的裁剪方块从滚动视图的下方开始。

代码:

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];

我在做什么导致图像在缩放时得到错误的高度?

4

2 回答 2

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];
}

加载原始图像:

在此处输入图像描述

缩放图像:

在此处输入图像描述

最终捕获图像

在此处输入图像描述

于 2013-04-04T06:35:53.107 回答
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];
于 2014-01-16T10:17:04.727 回答