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 に答える