iOS显示UIImage全屏,启用缩放(捏合和双击)

时间:2014-02-05 14:23:18

标签: ios iphone objective-c uiimageview uiimage

我使用UIImage从相机中捕获UIImagePickerController

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

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

self.imageView.contentMode = UIViewContentModeScaleAspectFill;

使图像全屏显示。但是我该如何实现缩放。我需要使用手势识别器从头开始吗?或者可能有一个默认的图像显示视图,其中所有实现的我都不知道?

2 个答案:

答案 0 :(得分:9)

我为我的某个应用创建了这个效果,别忘了设置你的滚动视图的委托。

<。ph。

的代码
#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
<。pm的代码。

#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];
}

答案 1 :(得分:3)

这很容易实现:

- (IBAction)handlePinch:(UIPinchGestureRecognizer *)recognizer;

然后:

- (IBAction)handlePinch:(UIPinchGestureRecognizer *)recognizer {    
recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale);
recognizer.scale = 1;    
}