问题描述
我有 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中显示捕获的图像
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全屏,启用缩放(捏合和双击)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!