在UIScrollView问题中裁剪缩放图像

时间:2014-07-29 12:39:41

标签: ios objective-c uiscrollview uiimage zoom

经过this link后,我的代码问题是输出图像无法设置正确的x和y值,因为裁剪后的图像似乎在结果图像中有0和0,无论我在哪里缩放(或者在哪里计算滚动偏移量)。这是我尝试过的。

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

2 个答案:

答案 0 :(得分:5)

与已发布的代码相似但只是在不传递UIScrollView

的情况下获取整个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)];
}

奖金:

裁剪方法

+(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;
}

答案 1 :(得分:4)

我正在使用以下代码,它完全符合预期:

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

即使内容被缩放也很有效,唯一必须明智地计算的是CGRect区域。