iOS訪問相簿及拍照儲存的實現執行個體

來源:互聯網
上載者:User

iOS訪問相簿及拍照儲存的實現執行個體

iOS訪問相簿及拍照儲存的實現執行個體

實現效果:
1、點擊訪問相簿按鈕,可以訪問系統相簿,選擇以後返回在imageView中顯示
2、點擊拍照,訪問網路攝影機,實現以後儲存在相簿中,返回在imageView中顯示

注意:拍照功能需要真機調試,模擬器無法實現
模擬器會有如下效果~彈出警告框

工程下載:github工程下載連結

下面是程式:注意此例中兩個button和UIimageView在stZ喎?http://www.bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vcnlib2FyZNbQzO2806O7PC9wPg0KPHA+Vmlld0NvbnRyb2xsZXIuaDwvcD4NCjxwcmUgY2xhc3M9"brush:java;">@interface ViewController : UIViewController@property (weak, nonatomic) IBOutlet UIImageView *imageShow;@end

ViewController.m

#pragma mark - 拍照並儲存- (IBAction)takePhotoAction:(id)sender {    BOOL isCamera = [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear];    if (!isCamera) { //若不可用,彈出警告框        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@無可用網路攝影機 message:nil delegate:self cancelButtonTitle:@確定 otherButtonTitles:nil, nil];        [alert show];        return;    }    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];    imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;    /**     *      UIImagePickerControllerSourceTypePhotoLibrary  ->所有資源檔夾            UIImagePickerControllerSourceTypeCamera        ->網路攝影機            UIImagePickerControllerSourceTypeSavedPhotosAlbum ->內建相簿     */    imagePicker.delegate = self;    //設定代理,遵循UINavigationControllerDelegate,UIImagePickerControllerDelegate協議    [self presentViewController:imagePicker animated:YES completion:nil];}
#pragma mark - 訪問相簿- (IBAction)browseAlbum:(id)sender {    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];    imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;    imagePicker.delegate = self;    [self presentViewController:imagePicker animated:YES completion:nil];}
#pragma mark - 協議方法的實現//協議方法,選擇完畢以後,呈現在imageShow裡面- (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];        self.imageShow.image = image;        //通過判斷picker的sourceType,如果是拍照則儲存到相簿去        if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {            UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);        }    }//  else  當然可能是視頻,這裡不作討論~方法是類似的~    [picker dismissViewControllerAnimated:YES completion:nil];}//此方法就在UIImageWriteToSavedPhotosAlbum的上方- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {    NSLog(@已儲存);}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.