1. 在xcode中新建一个基于view的工程,命名为PicViewer
2.修改PicViewerViewController.h:
#import <UIKit/UIKit.h>
@interface PicViewerViewController : UIViewController<UIGestureRecognizerDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate> {
IBOutletUIImageView *imageView;
}
@property (nonatomic, retain) IBOutlet UIImageView *imageView;
@end
3.在PicViewerController.m中添加如下一行:
@synthesize imageView;
4.打开PicViewerViewController.xib,拖一个UIImageView到View窗口中,并建立与imageView的连接
5.在PicViewerViewController.m中添加如下内容
#pragma mark -
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
imageView.image = [info objectForKey:UIImagePickerControllerOriginalImage];
[picker dismissModalViewControllerAnimated:YES];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[picker dismissModalViewControllerAnimated:YES];
}
6.在PicViewerViewController.m添加如下内容
#pragma mark -
#pragma mark Gesture Handler
- (void)resetImageView:(UITapGestureRecognizer *)recognizer {
[UIImageViewbeginAnimations:nilcontext:NULL];
[UIImageViewsetAnimationDuration:1];
[UIImageViewsetAnimationBeginsFromCurrentState:YES];
imageView.transform = CGAffineTransformIdentity;
[UIImageViewcommitAnimations];
}
- (void)handlePinchFrom:(UIPinchGestureRecognizer *)recognizer {
float scaleFactor = [recognizer scale];
imageView.transform = CGAffineTransformMakeScale(scaleFactor, scaleFactor);
}
- (void)handlePanFrom:(UIPanGestureRecognizer *)recognizer {
CGPoint trans = [recognizer translationInView:imageView];
imageView.transform = CGAffineTransformMakeTranslation(trans.x, trans.y);
}
- (void)selectPicture:(UILongPressGestureRecognizer *)recognizer{
if ([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
UIImagePickerController *picker = [[UIImagePickerControlleralloc]init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[selfpresentModalViewController:picker animated:YES];
[picker release];
}
else {
UIAlertView *alert = [[UIAlertViewalloc]
initWithTitle:@"Error accessing photo library"
message:@"Device does not support a photo library"
delegate:nil
cancelButtonTitle:@"Cancel"
otherButtonTitles:nil];
[alert show];
[alert release];
}
}
7.修改viewDidLoad与dealloc如下
- (void)viewDidLoad {
[superviewDidLoad];
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizeralloc] initWithTarget:selfaction:@selector(resetImageView:)];
tapRecognizer.numberOfTapsRequired = 2;
[self.imageView addGestureRecognizer:tapRecognizer];
tapRecognizer.delegate = self;
[tapRecognizer release];
UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizeralloc] initWithTarget:self
action:@selector(handlePinchFrom:)];
[self.imageView addGestureRecognizer:pinchRecognizer];
pinchRecognizer.delegate = self;
[pinchRecognizer release];
UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizeralloc] initWithTarget:self action:@selector(selectPicture:)];
[self.imageView addGestureRecognizer:longPressRecognizer];
longPressRecognizer.delegate = self;
[longPressRecognizer release];
UIPanGestureRecognizer *panGestureReconizer = [[UIPanGestureRecognizeralloc] initWithTarget:selfaction:@selector(handlePanFrom:)];
[self.imageView addGestureRecognizer:panGestureReconizer];
panGestureReconizer.delegate = self;
[panGestureReconizer release];
[selfperformSelector:@selector(selectPicture:) withObject:nilafterDelay:0.5f];
}
- (void)dealloc {
[imageViewrelease];
[super dealloc];
}