IOS开发之小实例--UIImagePickerController
生活随笔
收集整理的這篇文章主要介紹了
IOS开发之小实例--UIImagePickerController
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
摘自:http://www.cnblogs.com/goodboy-heyang/p/5403947.html
前言:本篇博文是本人閱讀國外的IOS Programming Tutorial的一篇入門文章的學習過程總結,難度不大,因為是入門。主要是入門UIImagePickerController這個控制器,那么這個 控制器是干嘛的呢?就是調用設備攝像機功能用的。到后面可能需要您在真機上測試,因為iPhone模擬器無法支持攝像機功能,運行測試會崩潰的哦。
?
網址:http://www.appcoda.com/ios-programming-camera-iphone-app/
1、首先簡單的創建一個工程,然后在storyboard和對應的.m文件中添加相關的代碼,這個簡明教程沒有使用自動布局,不多說,看圖識字:
?
2、下面是這個ViewController.m的完整實現:
#import "ViewController.h"@interface ViewController () <UIImagePickerControllerDelegate,UINavigationControllerDelegate>@property (strong, nonatomic) IBOutlet UIImageView *imageView;@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];// 這段代碼會自動判斷當前設備是否有攝像機功能,如果沒有,會彈窗提示if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Error"message:@"Device has no camera"delegate:nilcancelButtonTitle:@"OK"otherButtonTitles: nil];[myAlertView show];} } - (IBAction)takePhotot:(UIButton *)sender {// 創建UIImagePickerController控制器對象UIImagePickerController *picker = [[UIImagePickerController alloc] init];picker.delegate = self;picker.allowsEditing = YES;picker.sourceType = UIImagePickerControllerSourceTypeCamera;[self presentViewController:picker animated:YES completion:nil]; } - (IBAction)selectPhoto:(UIButton *)sender {// 創建UIImagePickerController控制器對象UIImagePickerController *picker = [[UIImagePickerController alloc] init];picker.delegate = self;picker.allowsEditing = YES;picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;[self presentViewController:picker animated:YES completion:nil]; } #pragma mark - 代理方法 -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{UIImage* chosenImage = info[UIImagePickerControllerEditedImage];self.imageView.image = chosenImage;[picker dismissViewControllerAnimated:YES completion:nil]; } -(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{[picker dismissViewControllerAnimated:YES completion:nil]; }@end?
轉載于:https://www.cnblogs.com/Lucky-056150/p/5404271.html
總結
以上是生活随笔為你收集整理的IOS开发之小实例--UIImagePickerController的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 管件广告词202个
- 下一篇: 192.168.8.1手机登陆路由器设置