2

我正在尝试UIImageUIImageView. UIImageView占据整个屏幕。捏和平移手势被添加到UIImageView。因此,用户可以平移/缩放图像视图。平移/缩放后,我只想裁剪图像视图的可见部分。我尝试了很多方法。但它返回了图像的错误部分。

提前致谢。

4

2 回答 2

10

将 QuartzCore 框架包含到您的项目中并实现此方法以从图像视图中获取图像的可见部分:(注意:我的意思是您在项目中使用 ARC)

#import <QuartzCore/QuartzCore.h>

- (UIImage*)imageFromImageView:(UIImageView*)imageView
{
    UIGraphicsBeginImageContext(imageView.frame.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextRotateCTM(context, 2*M_PI);

    [imageView.layer renderInContext:context];
    UIImage *image =  UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}
于 2012-06-20T14:42:18.970 回答
1

谢谢你们。我已经想出了解决方案。这是我用来获取图像可见部分的代码。(请记住,图像在缩放或平移时不完全可见,因为它占据了整个屏幕)

  - (UIImage *)cropVisiblePortionOfImageView:(UIImageView *)imageView {

  CGFloat zoomScaleX=imageView.frame.size.width/initialWidth;
  CGFloat zoomScaleY=imageView.frame.size.height/initialHeight;
  CGSize zoomedSize=CGSizeMake(initialWidth*zoomScaleX,initialHeight*zoomScaleY);

  UIGraphicsBeginImageContext(zoomedSize);
  [imageView.image drawInRect:CGRectMake(0, 0, zoomedSize.width, zoomedSize.height)];
  UIImage *zoomedImage = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();

  UIGraphicsBeginImageContext(CGSizeMake(initialWidth, initialHeight));
  [zoomedImage drawAtPoint:CGPointMake(imageView.frame.origin.x, imageView.frame.origin.y)];
  UIImage *cropedImage = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();

  return cropedImage;
}
于 2012-06-21T11:18:44.087 回答