調用系統的相機上傳照片

來源:互聯網
上載者:User

標籤:

  • -(void)tapGestureRecognizer:(UITapGestureRecognizer *)sender {    UIActionSheet *action = [[UIActionSheet alloc] initWithTitle:@"選擇照片" delegate: self cancelButtonTitle:@"取消" destructiveButtonTitle:@"相機" otherButtonTitles:@"本地相簿", nil];    [action showInView:self.view];}- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {    if (buttonIndex == 2) {        return;    }    //建立圖片選取器    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];    imagePicker.delegate = self;    //設定圖片選擇屬性    imagePicker.allowsEditing = NO;      if (buttonIndex == 0) {               if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {            //真機開啟            imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;        }else {                    //模擬器開啟            NSLog(@"模擬器開啟");            return;        }            }else {        //相簿        imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;    }    //進去選取器    [self presentViewController:imagePicker animated:YES completion:nil];    self.modalPresentationStyle = UIModalPresentationOverCurrentContext;    }#pragma mark - UIImagePickerControllerDelegate- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {    NSString *type = [info objectForKey:UIImagePickerControllerMediaType];        //當選擇的類型是圖片    if ([type isEqualToString:@"public.image"])    {        //先把圖片轉成NSData        UIImage* image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];        NSData *data;        if (UIImagePNGRepresentation(image) == nil)        {            data = UIImageJPEGRepresentation(image, 1.0);        }        else        {            data = UIImagePNGRepresentation(image);        }                self.headImg.image = image;                //圖片儲存的路徑        //這裡將圖片放在沙箱的documents檔案夾中        NSString * DocumentsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];                //檔案管理工具        NSFileManager *fileManager = [NSFileManager defaultManager];                //把剛剛圖片轉換的data對象拷貝至沙箱中 並儲存為image.png        [fileManager createDirectoryAtPath:DocumentsPath withIntermediateDirectories:YES attributes:nil error:nil];        [fileManager createFileAtPath:[DocumentsPath stringByAppendingString:@"/image.png"] contents:data attributes:nil];                //得到選擇後沙箱中圖片的完整路徑        self.filePath = [[NSString alloc]initWithFormat:@"%@%@",DocumentsPath,  @"/image.png"];                //關閉相簿介面        [picker dismissViewControllerAnimated:YES completion:nil];                //[self saveChangeData];    }    }//#pragma mark - delegate//- (void)saveChangeData {    //NSData *imagData =UIImagePNGRepresentation(self.headImg.image);    //NSString *imageStr = [[NSString alloc] initWithData:imagData encoding:NSUTF8StringEncoding];    //NSLog(@"0------%@",imageStr);    //[DataManager getInstance].user.head_image = imageStr;//}

     

調用系統的相機上傳照片

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.