0

After going through this link, issue with my code is that output image is unable to set proper x and y values as cropped image seems to have 0 and 0 in the resultant image irrespective to where I zoom (or where the scroll offset is calculated). Here's what I tried.

- (IBAction)crop:(id)sender
{

    float zoomScale = 1.0f / [self.scroll zoomScale];

    CGRect rect;
    NSLog(@"contentOffset is :%f,%f",[self.scroll contentOffset].x,[self.scroll contentOffset].y);
    rect.origin.x = self.scroll.contentOffset.x * zoomScale;
    rect.origin.y = self.scroll.contentOffset.y * zoomScale;
    rect.size.width = self.scroll.bounds.size.width * zoomScale;
    rect.size.height = self.scroll.bounds.size.height * zoomScale;

    UIGraphicsBeginImageContextWithOptions( CGSizeMake(rect.size.width, rect.size.height),
                                           NO,
                                           0.);

    NSLog(@"rect offset is :%f,%f",rect.origin.x,rect.origin.y);
    CGPoint point = CGPointMake(-rect.origin.x, -rect.origin.y); **//even though above NSLog have some values, but output image is unable to set proper x and y values as cropped image seems to have 0 and 0 in the resultant image.**
    [[self.imagV image] drawAtPoint:point
                          blendMode:kCGBlendModeCopy
                              alpha:1];
    self.croppedImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

DTImageViewController *imageViewController = [[DTImageViewController alloc] initWithNibName:@"DTImageViewController" bundle:nil];
    imageViewController.image = self.croppedImage;
    [self.navigationController pushViewController:imageViewController animated:YES];
}
Community
  • 1
  • 1
Deepak Thakur
  • 3,453
  • 2
  • 37
  • 65

2 Answers2

6

Similar code as already posted but just taking whole UIScrollView bounds without passing a CGRect

-(void)takeScreenShotOfScrollView
{
    UIGraphicsBeginImageContextWithOptions(scrollView.bounds.size, YES, [UIScreen mainScreen].scale);
    CGPoint offset = scrollView.contentOffset;
    CGContextTranslateCTM(UIGraphicsGetCurrentContext(), -offset.x, -offset.y);

    [scrollView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    img = [SDImageHelper imageWithImage:img_image scaledToSize:CGSizeMake(769, 495)];
}

BONUS:

The cropping method

+(UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize
{
    //UIGraphicsBeginImageContext(newSize);
    // In next line, pass 0.0 to use the current device's pixel scaling factor (and thus account for Retina resolution).
    // Pass 1.0 to force exact pixel size.
    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}
Jasper
  • 7,031
  • 3
  • 35
  • 43
4

I'm using the following code for this and it does exactly what expected:

- (UIImage *) croppedImageOfView:(UIView *) view withFrame:(CGRect) rect
{
    UIGraphicsBeginImageContextWithOptions(rect.size,NO,0.0);
    CGContextTranslateCTM(UIGraphicsGetCurrentContext(), -rect.origin.x, -rect.origin.y);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *visibleScrollViewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return visibleScrollViewImage;
}

- (void) crop
{
    CGRect neededRect = CGRectMake(50,50,100,100);
    UIImage *image = [self croppedImageOfView:_scrollView withFrame:neededRect];
}

Works well even if content is zoomed, the only thing that must be calculated wisely is needed area CGRect.

iOS Dev
  • 4,143
  • 5
  • 30
  • 58