iOS 显示 UIImage 全屏并启用缩放(捏合和双击) [英] iOS show UIImage full screen with zooming (pinch and double tap) enabled

查看:11
本文介绍了iOS 显示 UIImage 全屏并启用缩放(捏合和双击)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 UIImagePickerController 从相机捕获的 UIImage.

I have an UIImage captured from the camera with UIImagePickerController.

现在,在用户单击它后,我希望它显示全屏,并能够使用捏合手势和双击手势来放大和缩小特定区域.换句话说,我想模仿ios默认图片浏览器的功能.

Now after the user clicks on it, I'd like it to show full screen and be able to to zoom it in and out using pinch gestures and also the double tap gesture to zoom in a particular area. In other words, I'd like to emulate what the ios's default image browser does.

我在 UIImageView 中显示捕获的图像:

I display the captured image in an UIImageView with:

self.imageView.contentMode = UIViewContentModeScaleAspectFill;

这会使图像全屏显示.但是我如何实现缩放.我需要使用手势识别器从头开始吗?或者也许有一个默认的图像显示视图,其中包含我不知道的所有实现?

which makes the image go full screen. But how do I implement zooming. Do I need to do it from scratch using gesture recognizers? Or maybe there's a default image display view with all that implemented I am not aware of?

推荐答案

我已经为我的一个应用创建了这个效果,不要忘记设置你的滚动视图的委托.

i have create this effect for one of my app, dont forget to set delegate of your scrollview.

.h 文件的代码

#import <UIKit/UIKit.h>

@interface ImageViewerController : UIViewController<UIScrollViewDelegate>

// The scroll view used for zooming.
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;

// The image view that displays the image.
@property (weak, nonatomic) IBOutlet UIImageView *imageView;

// The image that will be shown.
@property (strong, nonatomic) NSString *imageUrlString;

@end

.m 的代码

#import "ImageViewerController.h"

@interface HNImageViewerController ()

- (IBAction)handleSingleTap:(UIButton*)tapGestureRecognizer;
@end

@implementation ImageViewerController

- (void)viewDidLoad {
[super viewDidLoad];
[self.imageView setImage:[UIImage imageNamed:@"placeholder-image"]];
self.scrollView.delegate=self;
}

- (BOOL)prefersStatusBarHidden {
return YES;
}

#pragma mark - UIScrollViewDelegate methods

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

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
if (self.scrollView.zoomScale == self.scrollView.minimumZoomScale) {
    [self dismissViewControllerAnimated:YES completion:nil];
}
}

#pragma mark - Private methods

- (IBAction)handleSingleTap:(UIButton *)tapGestureRecognizer {

[self dismissViewControllerAnimated:YES completion:nil];
}

这篇关于iOS 显示 UIImage 全屏并启用缩放(捏合和双击)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆