天天看點

iOS 打開iPhone照相機和相冊

一.添加代理

<span style="font-family:KaiTi_GB2312;font-size:18px;color:#333333;"><UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate></span>
           

二.添加一個顯示圖檔的按鈕

<span style="font-family:KaiTi_GB2312;font-size:18px;color:#333333;">- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = CGRectMake(100, 100, 80, 80);
    button.backgroundColor = [UIColor redColor];
    [button addTarget:self action:@selector(addImage) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}

- (void)addImage
{
    UIActionSheet* sheet = [[UIActionSheet alloc] initWithTitle:@"選擇圖檔" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"從攝像頭",@"從圖檔庫",nil];
    [sheet showInView:self.view];
}</span>
           

三.UIActionSheetDelegate,對應的打開照相機和相冊

#pragma mark -- UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch (buttonIndex) {
        case 0:
        {
            //拍照
            //資源類型為照相機
            UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
            //判斷是否有相機
            if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]){
                UIImagePickerController *picker = [[UIImagePickerController alloc] init];
                picker.delegate = self;
                //設定拍照後的圖檔可被編輯
                picker.allowsEditing = YES;
                //資源類型為照相機
                picker.sourceType = sourceType;
                [self presentViewController:picker animated:YES completion:nil];
                
            }else {
                NSLog(@"該裝置無攝像頭");
            }
        }
            
            break;
        case 1:
        {
            //從相冊選擇
            UIImagePickerController *picker = [[UIImagePickerController alloc] init];
            //資源類型為圖檔庫
            picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            picker.delegate = self;
            //設定選擇後的圖檔可被編輯
            picker.allowsEditing = YES;
            //    [self presentModalViewController:picker animated:YES];
            [self presentViewController:picker animated:YES completion:nil];
        }
            break;
        default:
            break;
    }
}
           

四.圖像選取器的委托方法,選完圖檔後回調該方法

<span style="font-family:KaiTi_GB2312;font-size:18px;color:#333333;">#pragma Delegate method UIImagePickerControllerDelegate
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo{
    
    //當圖檔不為空時顯示圖檔并儲存圖檔
    if (image != nil) {
        //圖檔顯示在界面上
        [button setBackgroundImage:image forState:UIControlStateNormal];
    }
    //關閉相冊界面
    [picker dismissModalViewControllerAnimated:YES];
}</span>
           

繼續閱讀