中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

ios 照相機(jī)和相冊(cè)的調(diào)用

2018-07-20    來源:編程學(xué)習(xí)網(wǎng)

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬(wàn)Linux鏡像隨意使用

一個(gè)簡(jiǎn)單的功能,上傳照片或者拍照可以用到.

//首先遵循兩個(gè)代理 <UIImagePickerControllerDelegate,UINavigationControllerDelegate> //我們創(chuàng)建一個(gè)btn和一個(gè)imageview,btn用來觸發(fā)事件調(diào)起照相機(jī)和相冊(cè)的功能,imageview用來展示選取或者拍攝的圖片. self.view.backgroundColor = [UIColor whiteColor]; UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(100 , 100, 200,50)];
    btn.backgroundColor = [UIColor blackColor];
    [btn setTitle:@"ChoosePhoto" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];

    _imageview = [[UIImageView alloc]initWithFrame:CGRectMake(200, 200, 100, 100)];
    _imageview.backgroundColor = [UIColor lightGrayColor];
    _imageview.layer.cornerRadius = CGRectGetHeight(_imageview.bounds)/2;
    _imageview.clipsToBounds = YES;

    [self.view addSubview:_imageview]; //btn的點(diǎn)擊事件 - (void)btnClick:(UIButton *)btn
{ //創(chuàng)建UIAlertController是為了讓用戶去選擇照片來源,拍照或者相冊(cè).  UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:0]; UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    imagePickerController.delegate = self;
    imagePickerController.allowsEditing = YES; UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"從相冊(cè)選取" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction *action) {

        imagePickerController.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;

        [self presentViewController:imagePickerController animated:YES completion:^{}];
   }]; UIAlertAction *photoAction = [UIAlertAction actionWithTitle:@"拍照" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction *action) {

        imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;

        [self presentViewController:imagePickerController animated:YES completion:^{}];
    }]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction *action)
    { //這里可以不寫代碼  }];
    [self presentViewController:alertController animated:YES completion:nil]; //用來判斷來源 Xcode中的模擬器是沒有拍攝功能的,當(dāng)用模擬器的時(shí)候我們不需要把拍照功能加速 if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])

    {

        [alertController addAction:okAction];
        [alertController addAction:cancelAction];
        [alertController addAction:photoAction];

    } else {
        [alertController addAction:okAction];
        [alertController addAction:cancelAction];
    }

} //這個(gè)是選取完照片后要執(zhí)行的代理方法  - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    [picker dismissViewControllerAnimated:YES completion:^{}]; //選取裁剪后的圖片 UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage]; /* 此處info 有六個(gè)值
     * UIImagePickerControllerMediaType; // an NSString UTTypeImage)
     * UIImagePickerControllerOriginalImage;  // a UIImage 原始圖片
     * UIImagePickerControllerEditedImage;    // a UIImage 裁剪后圖片
     * UIImagePickerControllerCropRect;       // an NSValue (CGRect)
     * UIImagePickerControllerMediaURL;       // an NSURL
     * UIImagePickerControllerReferenceURL    // an NSURL that references an asset in the AssetsLibrary framework
     * UIImagePickerControllerMediaMetadata    // an NSDictionary containing metadata from a captured photo
     */ _imageview.image = image;

}

標(biāo)簽: 代碼

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:iOS 內(nèi)存管理

下一篇:關(guān)于App程序員泡沫