淺談iPhone手機拍照功能和照片庫選取功能調用
作者:佚名
本文介紹的是淺談iPhone手機拍照功能和照片庫選取功能調用,主要是基于代碼實現,我們來看代碼。
淺談iPhone手機拍照功能和照片庫選取功能調用是本文要介紹的內容,內容不多,主要是以代碼實現,我們來看內容。
首先,你得實現UIImagePickerControllerDelegate,UINavigationControllerDelegate兩個代理
其次定義按鈕:
- // 從相簿里上傳一張
- UIButton *btn1 = [UIButton buttonWithType: UIButtonTypeRoundedRect];
- btn1.frame = CGRectMake(60.0, 237.0, 200.0, 32.0);
- [btn1 setTitle:@"從媒體庫中選取圖片" forState:UIControlStateNormal];
- [btn1 addTarget:self action:@selector(picker) forControlEvents:UIControlEventTouchUpInside];
- UIButton *btn2 = [UIButton buttonWithType: UIButtonTypeRoundedRect];
- btn2.frame = CGRectMake(60.0, 290.0, 200.0, 32.0);
- [btn2 setTitle:@"從相機獲取圖片" forState:UIControlStateNormal];
- [btn2 addTarget:self action:@selector(picker1) forControlEvents:UIControlEventTouchUpInside];
實現按鈕方法:
- // 從照片庫里面選取
- -(void)picker
- {
- imagepicker = [[UIImagePickerController alloc] init];
- imagepicker.delegate = self;
- imagepicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
- imagepicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
- imagepicker.allowsEditing = YES;
- [self presentModalViewController:self.imagepicker animated:YES];
- }
- // 從相機獲取
- -(void)picker1
- {
- imagepicker = [[UIImagePickerController alloc] init];
- imagepicker.delegate = self;
- imagepicker.sourceType = UIImagePickerControllerSourceTypeCamera;
- imagepicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
- imagepicker.allowsEditing = YES;
- [self presentModalViewController:self.imagepicker animated:YES];
- }
實現代理方法:
- // 初始化選取
- - (void)imagePickerController:(UIImagePickerController *)picker
- didFinishPickingImage:(UIImage *)image
- editingInfo:(NSDictionary *)editingInfo
- {
- myImageView.image = image;
- [[picker parentViewController] dismissModalViewControllerAnimated:YES];
- }
- // 完成選取
- - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
- {
- [picker dismissModalViewControllerAnimated:YES];
- }
小結:淺談iPhone手機拍照功能和照片庫選取功能調用的內容介紹完了,希望本文對你有所幫助
責任編輯:zhaolei
來源:
互聯網