用户捏屏时如何放大/缩小UIImage对象?


84

当用户在我的应用程序上执行标准收缩操作时,我想放大/缩小UIImage对象。我目前正在使用UIImageView来显示我的图像,如果该细节有任何帮助的话。

我正在尝试弄清楚该如何做,但是到目前为止还没有运气。

有什么线索吗?

Answers:


85

另一种简单的方法是将您UIImageView放在UIScrollView正如我在这里描述的,您需要将滚动视图的contentSize设置为与您的UIImageView's大小相同。将您的控制器实例设置为滚动视图的委托,并实现viewForZoomingInScrollView:scrollViewDidEndZooming:withView:atScale:方法以允许捏缩放和图像平移。这实际上是Ben的解决方案所做的事情,只是以稍微更轻巧的方式,因为您没有完整Web视图的开销。

您可能会遇到的一个问题是,滚动视图中的缩放比例是以应用于图像的变换的形式出现的。这可能导致在高变焦倍率下模糊。对于可以重绘的内容,您可以按照我的建议捏捏手势完成后提供更清晰的显示。此时可以使用hniels的解决方案来重新缩放图像。


2
确保更改了maximumZoomScale和/或minimumZoomScale :)。
克里斯·普林斯

94

如其他人所述,最简单的解决方案是将UIImageView放入UIScrollView。我是在Interface Builder .xib文件中完成的。

在viewDidLoad中,设置以下变量。将您的控制器设置为UIScrollViewDelegate。

- (void)viewDidLoad {
    [super viewDidLoad];
    self.scrollView.minimumZoomScale = 0.5;
    self.scrollView.maximumZoomScale = 6.0;
    self.scrollView.contentSize = self.imageView.frame.size;
    self.scrollView.delegate = self;
}

您需要实现以下方法来返回要缩放的imageView。

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return self.imageView;
}

在iOS9之前的版本中,您可能还需要添加以下空的委托方法:

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale
{
}

苹果文档确实描述了如何做到这一点的一个很好的工作:


3
可以删除scrollViewDidEndZooming:withView:现在,我已经使用iOS9 Xcode7进行了测试
ElonChan

1
这个对我有用!没有实现scrollViewDidEndZooming :方法。
约翰尼·张

48

Shefali针对UIImageView的解决方案效果很好,但需要进行一些修改:

- (void)pinch:(UIPinchGestureRecognizer *)gesture {
    if (gesture.state == UIGestureRecognizerStateEnded
        || gesture.state == UIGestureRecognizerStateChanged) {
        NSLog(@"gesture.scale = %f", gesture.scale);

        CGFloat currentScale = self.frame.size.width / self.bounds.size.width;
        CGFloat newScale = currentScale * gesture.scale;

        if (newScale < MINIMUM_SCALE) {
            newScale = MINIMUM_SCALE;
        }
        if (newScale > MAXIMUM_SCALE) {
            newScale = MAXIMUM_SCALE;
        }

        CGAffineTransform transform = CGAffineTransformMakeScale(newScale, newScale);
        self.transform = transform;
        gesture.scale = 1;
    }
}

(Shefali的解决方案有一个缺点,即在捏合时不会连续缩放。此外,在开始新的捏合时,会重置当前图像比例。)


我要在捏起手势后启用平移手势,该怎么做?
Gajendra K Chauhan 2012年

@Gajendra:在这种情况下,我认为您应该使用在滚动视图中嵌入组件的方式,这可以同时支持这两种功能
JRV 2012年

但是我想要同时具有轻击手势和捏合手势的功能。我还在Imageview上使用了网格(图层)。我是否知道,是否可以在滚动视图中使用捏合手势和点击手势。
Gajendra K Chauhan 2012年

@GajendraKChauhan:滚动视图支持点按缩放和平移开箱即用-仅记得设置最小/最大缩放比例,但也请查看Brad Larsons的答案。而且,如果您还想要轻击手势,则可以将其添加为手势。
JRV

@JRV,我需要添加哪些代表才能使self.frame.size.width被识别?

23

下面的代码有助于在不使用UIScrollView的情况下缩放UIImageView:

-(void)HandlePinch:(UIPinchGestureRecognizer*)recognizer{
    if ([recognizer state] == UIGestureRecognizerStateEnded) {
        NSLog(@"======== Scale Applied ===========");
        if ([recognizer scale]<1.0f) {
            [recognizer setScale:1.0f];
        }
        CGAffineTransform transform = CGAffineTransformMakeScale([recognizer scale],  [recognizer scale]);
        imgView.transform = transform;
    }
}

这是最高的解决方案:)。谢谢。
萨比兰

19

请记住,您永远不会放大UIImage。永远

相反,您正在放大和缩小view显示的UIImage

在这种特殊情况下,您应该选择UIView使用自定义图形来创建一个自定义图形来显示图像,一个UIImageView将为您显示图像,或者一个UIWebView将需要一些其他HTML来备份它。

在所有情况下,您都需要实现touchesBegantouchesMoved和等来确定用户正在尝试执行的操作(缩放,平移等)。


+1为澄清起见,没想到会这样,欢呼声哥们
-Elmo

@八月,您能给我提供一些示例吗,该示例可以在touchesBegan,touchesMoved上缩放和旋转图像吗?
Mitesh Dobareeya '16

7

这是我之前使用过的解决方案,不需要您使用UIWebView。

- (UIImage *)scaleAndRotateImage(UIImage *)image
{
    int kMaxResolution = 320; // Or whatever

    CGImageRef imgRef = image.CGImage;

    CGFloat width = CGImageGetWidth(imgRef);
    CGFloat height = CGImageGetHeight(imgRef);


    CGAffineTransform transform = CGAffineTransformIdentity;
    CGRect bounds = CGRectMake(0, 0, width, height);
    if (width > kMaxResolution || height > kMaxResolution) {
        CGFloat ratio = width/height;
        if (ratio > 1) {
            bounds.size.width = kMaxResolution;
            bounds.size.height = bounds.size.width / ratio;
        }
        else {
            bounds.size.height = kMaxResolution;
            bounds.size.width = bounds.size.height * ratio;
        }
    }

    CGFloat scaleRatio = bounds.size.width / width;
    CGSize imageSize = CGSizeMake(CGImageGetWidth(imgRef), CGImageGetHeight(imgRef));
    CGFloat boundHeight;
    UIImageOrientation orient = image.imageOrientation;
    switch(orient) {

        case UIImageOrientationUp: //EXIF = 1
            transform = CGAffineTransformIdentity;
            break;

        case UIImageOrientationUpMirrored: //EXIF = 2
            transform = CGAffineTransformMakeTranslation(imageSize.width, 0.0);
            transform = CGAffineTransformScale(transform, -1.0, 1.0);
            break;

        case UIImageOrientationDown: //EXIF = 3
            transform = CGAffineTransformMakeTranslation(imageSize.width, imageSize.height);
            transform = CGAffineTransformRotate(transform, M_PI);
            break;

        case UIImageOrientationDownMirrored: //EXIF = 4
            transform = CGAffineTransformMakeTranslation(0.0, imageSize.height);
            transform = CGAffineTransformScale(transform, 1.0, -1.0);
            break;

        case UIImageOrientationLeftMirrored: //EXIF = 5
            boundHeight = bounds.size.height;
            bounds.size.height = bounds.size.width;
            bounds.size.width = boundHeight;
            transform = CGAffineTransformMakeTranslation(imageSize.height, imageSize.width);
            transform = CGAffineTransformScale(transform, -1.0, 1.0);
            transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);
            break;

        case UIImageOrientationLeft: //EXIF = 6
            boundHeight = bounds.size.height;
            bounds.size.height = bounds.size.width;
            bounds.size.width = boundHeight;
            transform = CGAffineTransformMakeTranslation(0.0, imageSize.width);
            transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);
            break;

        case UIImageOrientationRightMirrored: //EXIF = 7
            boundHeight = bounds.size.height;
            bounds.size.height = bounds.size.width;
            bounds.size.width = boundHeight;
            transform = CGAffineTransformMakeScale(-1.0, 1.0);
            transform = CGAffineTransformRotate(transform, M_PI / 2.0);
            break;

        case UIImageOrientationRight: //EXIF = 8
            boundHeight = bounds.size.height;
            bounds.size.height = bounds.size.width;
            bounds.size.width = boundHeight;
            transform = CGAffineTransformMakeTranslation(imageSize.height, 0.0);
            transform = CGAffineTransformRotate(transform, M_PI / 2.0);
            break;

        default:
            [NSException raise:NSInternalInconsistencyException format:@"Invalid image orientation"];

    }

    UIGraphicsBeginImageContext(bounds.size);

    CGContextRef context = UIGraphicsGetCurrentContext();

    if (orient == UIImageOrientationRight || orient == UIImageOrientationLeft) {
        CGContextScaleCTM(context, -scaleRatio, scaleRatio);
        CGContextTranslateCTM(context, -height, 0);
    }
    else {
        CGContextScaleCTM(context, scaleRatio, -scaleRatio);
        CGContextTranslateCTM(context, 0, -height);
    }

    CGContextConcatCTM(context, transform);

    CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, width, height), imgRef);
    UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return imageCopy;
}

可以在Apple支持上找到该文章,网址为:http : //discussions.apple.com/message.jspa?messageID=7276709#7276709


-1为jsp托管支持页面。点语法动摇拳头
esharp 2014年

您能否提供给我如何调用此方法的方法(通过鼠标移动或触摸)?
Bhumesh Purohit

5

Shafali和JRV的答案扩展到包括平移和捏缩放。

#define MINIMUM_SCALE 0.5
#define MAXIMUM_SCALE 6.0
@property CGPoint translation;


- (void)pan:(UIPanGestureRecognizer *)gesture {
    static CGPoint currentTranslation;
    static CGFloat currentScale = 0;
    if (gesture.state == UIGestureRecognizerStateBegan) {
        currentTranslation = _translation;
        currentScale = self.view.frame.size.width / self.view.bounds.size.width;
    }
    if (gesture.state == UIGestureRecognizerStateEnded || gesture.state == UIGestureRecognizerStateChanged) {

        CGPoint translation = [gesture translationInView:self.view];

        _translation.x = translation.x + currentTranslation.x;
        _translation.y = translation.y + currentTranslation.y;
        CGAffineTransform transform1 = CGAffineTransformMakeTranslation(_translation.x , _translation.y);
        CGAffineTransform transform2 = CGAffineTransformMakeScale(currentScale, currentScale);
        CGAffineTransform transform = CGAffineTransformConcat(transform1, transform2);
        self.view.transform = transform;
    }
}


- (void)pinch:(UIPinchGestureRecognizer *)gesture {
    if (gesture.state == UIGestureRecognizerStateEnded || gesture.state == UIGestureRecognizerStateChanged) {
//        NSLog(@"gesture.scale = %f", gesture.scale);

        CGFloat currentScale = self.view.frame.size.width / self.view.bounds.size.width;
        CGFloat newScale = currentScale * gesture.scale;

        if (newScale < MINIMUM_SCALE) {
            newScale = MINIMUM_SCALE;
        }
        if (newScale > MAXIMUM_SCALE) {
            newScale = MAXIMUM_SCALE;
        }

        CGAffineTransform transform1 = CGAffineTransformMakeTranslation(_translation.x, _translation.y);
        CGAffineTransform transform2 = CGAffineTransformMakeScale(newScale, newScale);
        CGAffineTransform transform = CGAffineTransformConcat(transform1, transform2);
        self.view.transform = transform;
        gesture.scale = 1;
    }
}

请避免提及其他人的答案,因为他们可以随时删除他们的答案。
Enamul Hassan

3

如果要做的最简单的方法是将图像放置在内部UIWebView(编写少量html包装器代码,引用您的图像就可以完成)。更复杂的方法是使用touchesBegantouchesMovedtouchesEnded跟踪用户的手指,并适当调整视图的transform属性。


您能建议我一些与您的答案有关的例子吗?如何使用touchesBegin,touchesMoved方法缩放和旋转图像。我的问题链接=> stackoverflow.com/questions/36833841/...
Mitesh Dobareeya

0

请记住,您不想放大/缩小UIImage。而是尝试放大/缩小包含UIImage视图控制器的视图。

我已经解决了这个问题。看一下我的代码:

@IBAction func scaleImage(sender: UIPinchGestureRecognizer) {
        self.view.transform = CGAffineTransformScale(self.view.transform, sender.scale, sender.scale)
        sender.scale = 1
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        view.backgroundColor = UIColor.blackColor()
    }

注意:别忘了挂上PinchGestureRecognizer。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.