CGContext - 缩放,然后在点处裁剪图像

时间:2014-12-16 21:55:48

标签: ios objective-c uiscrollview uiimageview crop

与Instagram类似,我有一个方形裁剪视图(UIScrollView),里面有一个UIImageView。因此,用户可以在方形矩形内拖动纵向或横向图像(等于屏幕的宽度),然后应在滚动偏移处裁剪图像。 UIImageView设置为纵横拟合。 UIScrollView内容大小设置为横向或纵向的比例因子,以便使用纵横比适当比例正确呈现。

当用户完成拖动时,我想根据给定的尺寸向上缩放图像,让我们说1000x1000px的平方,然后在滚动偏移处裁剪它(使用[UIImage drawAtPoint:CGPoint]。

问题是我无法获得正确的偏移点来获得正确的偏移点。如果我在6+上接近它就会在4S上接近它。

这是我的规模和作物代码:

(UIImage *)squareImageFromImage:(UIImage *)image scaledToSize:(CGFloat)newSize {
CGAffineTransform scaleTransform;
CGPoint origin;

if (image.size.width > image.size.height) {
    //landscape
    CGFloat scaleRatio = newSize / image.size.height;
    scaleTransform = CGAffineTransformMakeScale(scaleRatio, scaleRatio);
    origin = CGPointMake((int)(-self.scrollView.contentOffset.x*scaleRatio),0);
} else if (image.size.width < image.size.height) {
    //portrait
    CGFloat scaleRatio = newSize / image.size.width;
    scaleTransform = CGAffineTransformMakeScale(scaleRatio, scaleRatio);
    origin = CGPointMake(0, (int)(-self.scrollView.contentOffset.y*scaleRatio));
} else {
    //square
    CGFloat scaleRatio = newSize / image.size.width;
    scaleTransform = CGAffineTransformMakeScale(scaleRatio, scaleRatio);
    origin = CGPointMake(0, 0);
}

UIGraphicsBeginImageContextWithOptions(size, YES, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextConcatCTM(context, scaleTransform);
[image drawAtPoint:origin];
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return image;
}

因此,例如对于横向,如果我向左拖动滚动以便将图像一直向右拖动,我的偏移量将接近6 +,但是在4S上它将关闭约150-200就CGPoint而言。

以下是设置滚动视图和图像视图的代码:

CGRect cropRect = CGRectMake(0.0f,0.0,SCREEN_WIDTH,SCREEN_WIDTH);
CGFloat ratio = (int)self.image.size.height/self.image.size.width;
CGRect r = CGRectMake(0.0,0.0,SCREEN_WIDTH,SCREEN_WIDTH);

if (ratio>1.00) {
    //portrait
    r = CGRectMake(0.0,0.0,SCREEN_WIDTH,(int)(SCREEN_WIDTH*ratio));
} else if (ratio<1.00) {
    //landscape
    CGFloat size = (int)self.image.size.width/self.image.size.height;
    cropOffset = (SCREEN_WIDTH*size)-SCREEN_WIDTH;
    r = CGRectMake(0.0,0.0,(int)(SCREEN_WIDTH*size),SCREEN_WIDTH);
}

NSLog(@"r.size.height == %.4f",r.size.height);
self.scrollView.frame = cropRect;
self.scrollView.contentSize = r.size;

self.imageView = [[UIImageView alloc] initWithFrame:r];
self.imageView.backgroundColor = [UIColor clearColor];
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
self.imageView.image = self.image;

[self.scrollView addSubview:self.imageView];

1 个答案:

答案 0 :(得分:0)

裁剪数学可能很棘手。我已经有一段时间不得不处理这个问题了,所以希望我能指出你正确的方向。以下是来自Pixology的一大块代码,用于从UIScrollView中获取缩放的可见rect。我认为这里缺少的成分可能是zoomScale

CGRect visibleRect;
visibleRect.origin = _scrollView.contentOffset;
visibleRect.size = _scrollView.bounds.size;
// figure in the scale
float theScale = 1.0 / _scrollView.zoomScale;
visibleRect.origin.x *= theScale;
visibleRect.origin.y *= theScale;
visibleRect.size.width *= theScale;
visibleRect.size.height *= theScale;

您可能还需要计算设备屏幕比例:

CGFloat screenScale = [[UIScreen mainScreen] scale];

了解您可以获取此信息的详细信息,并告知我们。