【问题标题】:How to get original image when we zoom in and zoom out the image in ios在ios中放大和缩小图像时如何获取原始图像
【发布时间】:2015-11-15 07:57:26
【问题描述】:

嗨,我是 ios 的初学者,在我的项目中,我在 UIScrollview 上添加 UIImage,我在 UIImage 上添加了点击手势

当我们双击 UIImage 时,图像应该在视图控制器上缩放全屏尺寸

在全屏大小的图像之后,我们可以按照我们想要的任何方式缩放它(我的意思是使用像捏缩放效果)这里我的要求是当我们双击图像时,图像需要设置它的原始位置我试过了水平最好,但我没有得到结果,请帮助我

我的代码如下:

#import "ViewController2.h"

@interface ViewController2 ()
{
    UIScrollView * myScroll;
    UITapGestureRecognizer *tap;
    BOOL isFullScreen;
    CGRect prevFrame;
    UIImageView * _imageView;
}

@end

@implementation ViewController2

- (void)viewDidLoad
{
    [super viewDidLoad];

    isFullScreen = FALSE;
    myScroll = [[UIScrollView alloc] init];
    myScroll.frame = self.view.bounds;
    myScroll.contentSize = CGSizeMake(_imageView.frame.size.width, _imageView.frame.size.height);
    myScroll.maximumZoomScale = 4.0;
    myScroll.minimumZoomScale = 1.0;
    myScroll.clipsToBounds = YES;
    myScroll.delegate = self;
    myScroll.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:myScroll];

    _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 300, 200)];
    _imageView.contentMode = UIViewContentModeScaleAspectFill;
    [_imageView setClipsToBounds:YES];
    _imageView.userInteractionEnabled = YES;
    _imageView.image = [UIImage imageNamed:@"ram.jpeg"];

    UITapGestureRecognizer *tapper = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imgToFullScreen:)];
    tapper.numberOfTapsRequired = 1;
    [_imageView addGestureRecognizer:tapper];
    [myScroll addSubview:_imageView];
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {

    return _imageView;
}

-(void)imgToFullScreen:(UITapGestureRecognizer*)sender {

    if (!isFullScreen) {

        [UIView animateWithDuration:0.5 delay:0 options:0 animations:^{

            prevFrame = _imageView.frame;
            [_imageView setFrame:[[UIScreen mainScreen] bounds]];
        }completion:^(BOOL finished){
            isFullScreen = TRUE;
        }];
        return;
    }
    else{
        [UIView animateWithDuration:0.5 delay:0 options:0 animations:^{
            [_imageView setFrame:prevFrame];
        }completion:^(BOOL finished){
            isFullScreen = FALSE;
        }];
        return;
    }
}

@end

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    我对您的代码进行了一些修改,如下所示尝试一下。检查它是否提供您想要的输出。

    @interface   ScrollViewController ()<UIScrollViewDelegate>{
            UIScrollView * myScroll;
            UITapGestureRecognizer *tap;
            BOOL isFullScreen;
            CGRect prevFrame;
            UIImageView * _imageView;
            CGAffineTransform trans;
        }
    
    
    
        @end
    
        @implementation ScrollViewController
    
        - (void)viewDidLoad {
            [super viewDidLoad];
            // Do any additional setup after loading the view from its nib.
    
            isFullScreen = FALSE;
            myScroll = [[UIScrollView alloc] init];
            myScroll.frame = [UIScreen mainScreen].bounds;
            myScroll.contentSize = CGSizeMake(prevFrame.size.width, prevFrame.size.height);
            myScroll.maximumZoomScale = 4.0;
            myScroll.minimumZoomScale = 1.0;
            myScroll.clipsToBounds = YES;
            myScroll.delegate = self;
            myScroll.backgroundColor = [UIColor lightGrayColor];
            [self.view addSubview:myScroll];
    
            _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 300, 200)];
            _imageView.contentMode = UIViewContentModeScaleAspectFill;
            [_imageView setClipsToBounds:YES];
            _imageView.userInteractionEnabled = YES;
            _imageView.image = [UIImage imageNamed:@"img.png"];
    
            UITapGestureRecognizer *tapper = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imgToFullScreen:)];
            tapper.numberOfTapsRequired = 2;
            [_imageView addGestureRecognizer:tapper];
            [myScroll addSubview:_imageView];
    
            prevFrame = _imageView.frame;
            trans = _imageView.transform;
        }
    
        - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    
            return _imageView;
        }
    
        -(void)imgToFullScreen:(UITapGestureRecognizer*)sender {
    
    
            if (!isFullScreen) {
                 myScroll.contentSize = prevFrame.size;
                [UIView animateWithDuration:0.5 delay:0 options:0 animations:^{
                    _imageView.frame = [UIScreen mainScreen].bounds;
    
                }completion:^(BOOL finished){
                    isFullScreen = TRUE;
    
                }];
                return;
            }
            else{
                [UIView animateWithDuration:0.5 delay:0 options:0 animations:^{
    
                    myScroll.contentSize = CGSizeMake(prevFrame.size.width, prevFrame.size.height);
                    _imageView.transform = trans;
                    _imageView.frame = prevFrame;
    
    
                    NSLog(@"%@ %@",NSStringFromCGSize(myScroll.contentSize),NSStringFromCGRect(prevFrame));
                }completion:^(BOOL finished){
                    isFullScreen = FALSE;
    
                }];
                return;
            }
        }
    

    【讨论】:

    • 但是当我缩放图像时它变得模糊我们如何解决这个问题
    猜你喜欢
    • 2012-01-24
    • 2017-06-10
    • 1970-01-01
    • 2012-08-28
    • 1970-01-01
    • 2011-06-08
    • 2012-06-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多