-(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;
//}
調(diào)用系統(tǒng)的相機(jī)上傳照片
?著作權(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ù)。
【社區(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)容
- 大家可以通過產(chǎn)看WebViewClient這個(gè)類里面的源碼可知,在下面的幾個(gè)方法里面5.0以下是通過ValueCa...
- 一般上傳圖片的步驟: 1:創(chuàng)建UIActionSheet,利用其代理方法判斷是調(diào)用相機(jī)還是相冊(cè)2:創(chuàng)建相機(jī)方法;3...
- 如題,相信很多開發(fā)者在調(diào)用系統(tǒng)照相機(jī)接收拍好的照片時(shí),發(fā)現(xiàn)照片被無故旋轉(zhuǎn)了90度。這一問題反映在大部分的三星手機(jī)上...
- 解決辦法: (得到的bmpOk就是正常的圖片) int degree = ImageUtil.readPictur...
- 一直以來, 我都不敢去觸碰自己的內(nèi)在孩童。 內(nèi)在孩童, 我是這樣理解的, 由幼時(shí)原生家庭問題造成的心理創(chuàng)傷,成長(zhǎng)過...