-(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;
//}
調用系統的相機上傳照片
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
- 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
- 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
- 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
推薦閱讀更多精彩內容
- 大家可以通過產看WebViewClient這個類里面的源碼可知,在下面的幾個方法里面5.0以下是通過ValueCa...
- 一般上傳圖片的步驟: 1:創建UIActionSheet,利用其代理方法判斷是調用相機還是相冊2:創建相機方法;3...
- 解決辦法: (得到的bmpOk就是正常的圖片) int degree = ImageUtil.readPictur...