1.新建一個single view工程
2.ViewController.h
@interface ViewController : UIViewController{ UIImageView *_imgView;//展示圖片 } @end
//初始化image view _imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)]; _imgView.backgroundColor = [UIColor yellowColor]; _imgView.userInteractionEnabled = YES;//一定要記得打開圖片視圖的用戶交互 [self.view addSubview:_imgView]; [_imgView release];
4.為_imgView添加手勢操作
//手勢 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectImage)]; // tap.numberOfTapsRequired = 2;//雙擊觸發 // tap.numberOfTouchesRequired = 2;//雙指觸摸觸發 [_imgView addGestureRecognizer:tap]; [tap release];
- (void)selectImage { UIImagePickerController *ipc = [[UIImagePickerController alloc] init]; ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;//設置從哪裡選擇圖片 ipc.delegate = self; [self presentViewController:ipc animated:YES completion:nil];//模態視圖 [ipc release]; }
//選擇圖片調用 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { _imgView.image = [info objectForKey:UIImagePickerControllerOriginalImage];// [self dismissViewControllerAnimated:YES completion:nil]; } //點擊取消調用 - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { [self dismissViewControllerAnimated:YES completion:nil]; }