iOS 開(kāi)發(fā)多媒體---相冊(cè)選取和拍照

關(guān)于 iOS 相冊(cè)選取和拍照

需要注意的是需要現(xiàn)在plist 文件中加入以下2個(gè)屬性
①.Privacy - Photo Library Usage Description
②.Privacy - Camera Usage Description
遵循2個(gè)代理方法
UIImagePickerControllerDelegate
UINavigationControllerDelegate

1.攝像頭

        //是否是攝像頭
        BOOL isCamera = [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear];
        if (!isCamera) { //若不可用,彈出警告框
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"無(wú)可用攝像頭" message:nil delegate:self cancelButtonTitle:@"確定" otherButtonTitles:nil, nil];
            [alert show];
            return;
        }
        UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
/**  
        *UIImagePickerControllerSourceTypePhotoLibrary  ->所有資源文件夾
         UIImagePickerControllerSourceTypeCamera        ->攝像頭
         UIImagePickerControllerSourceTypeSavedPhotosAlbum ->內(nèi)置相冊(cè)
         */
        
//設(shè)置代理,遵循UINavigationControllerDelegate,UIImagePickerControllerDelegate協(xié)議
        imagePicker.delegate = self;
        [self presentViewController:imagePicker animated:YES completion:nil];

2.訪問(wèn)相冊(cè)

UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
        imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
        imagePicker.delegate = self;
        [self presentViewController:imagePicker animated:YES completion:nil];

3.代理方法

#pragma mark - 協(xié)議方法的實(shí)現(xiàn)
//協(xié)議方法,選擇完畢以后,顯示在 cell 里面
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    NSLog(@"%@",info);  //UIImagePickerControllerMediaType,UIImagePickerControllerOriginalImage,UIImagePickerControllerReferenceURL
    NSString *mediaType = info[@"UIImagePickerControllerMediaType"];
    if ([mediaType isEqualToString:@"public.image"]) {  //判斷是否為圖片
        
        UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
   
        imageCell.imageView.image = image;
        
        //通過(guò)判斷picker的sourceType,如果是拍照則保存到相冊(cè)去
        if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
            UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
        }
    }
    [picker dismissViewControllerAnimated:YES completion:nil];
}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
    NSLog(@"已保存");
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容