調(diào)用系統(tǒng)的相機(jī)上傳照片

-(void)tapGestureRecognizer:(UITapGestureRecognizer *)sender {

    UIActionSheet *action = [[UIActionSheet alloc] initWithTitle:@"選擇照片" delegate: self cancelButtonTitle:@"取消" destructiveButtonTitle:@"相機(jī)" otherButtonTitles:@"本地相冊(cè)", nil];
    [action showInView:self.view];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {

    if (buttonIndex == 2) {
        return;
    }

    //創(chuàng)建圖片選擇器
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.delegate = self;
    //設(shè)置圖片選擇屬性
    imagePicker.allowsEditing = NO;
  
    if (buttonIndex == 0) {
       
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            //真機(jī)打開
            imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        }else {
        
            //模擬器打開
            NSLog(@"模擬器打開");
            return;
        }
        
    }else {
        //相冊(cè)
        imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    }
    //進(jìn)去選擇器
    [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];
    
    //當(dāng)選擇的類型是圖片
    if ([type isEqualToString:@"public.image"])
    {
        //先把圖片轉(zhuǎn)成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];
        
        //把剛剛圖片轉(zhuǎn)換的data對(duì)象拷貝至沙盒中 并保存為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"];
        
        //關(guān)閉相冊(cè)界面
        [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;
//}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容