oc获得相册照片

 1 - (void)addImage
 2 {
 3     if (CGRectGetMaxX(addImageView.frame)>SCREEN_WIDTH-CGRectGetWidth(addImageView.frame)) {
 4         [self ShowHUDTitle:@"只能添加三张图片" andDelay:2];
 5         return;
 6     }
 7     UIActionSheet *actionsheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles: @"打开照相机", @"从手机相册获取",nil];
 8     
 9     [actionsheet showInView:self.view];
10 }
11 
12 
13 - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
14 {
15     if (buttonIndex == actionSheet.cancelButtonIndex) {
16     }
17     switch (buttonIndex) {
18         case 0://打开照相机
19             [self takePhoto];
20             break;
21         case 1://打开相册
22             [self localPhoto];
23             break;
24         default:
25             break;
26     }
27 }
28 
29 -(void)takePhoto//打开相机
30 {
31     UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
32     if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
33         UIImagePickerController *picker = [[UIImagePickerController alloc]init];
34         picker.delegate = self;
35         picker.allowsEditing = YES;
36         picker.sourceType = sourceType;
37         [self presentViewController:picker animated:YES completion:nil];
38     }
39 }
40 -(void)localPhoto//本地相册
41 {
42     UIImagePickerController *picker = [[UIImagePickerController alloc]init];
43     picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
44     picker.delegate = self;
45     picker.allowsEditing = YES;
46     [self presentViewController:picker animated:YES completion:nil];
47 }
48 
49 
50 -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
51 {
52     NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
53 //      当选择的类型是图片
54     if ([type isEqualToString:@"public.image"]) {
55 //      把图片转化为NSData
56         UIImage *image = info[UIImagePickerControllerEditedImage];
57         NSData *data = UIImageJPEGRepresentation(image, 0.03);
58         
59         
60 //      图片的保存路径
61         NSString *documentsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
62         NSFileManager *manager = [NSFileManager defaultManager];
63         
64 //      将刚刚转化的图片放到沙盒中 并保存为png
65         [manager createDirectoryAtPath:documentsPath withIntermediateDirectories:YES attributes:nil error:nil];
66         NSString *imageName = [NSString stringWithFormat:@"/%@.png",[NSDate date]];
67        
68         [manager createFileAtPath:[documentsPath stringByAppendingString:[NSString stringWithFormat:@"%@",imageName]] contents:data attributes:nil];
69         
70 //得到选择后沙盒的路径
71         filePath = [[NSString alloc]initWithFormat:@"%@%@",documentsPath,imageName];
72         NSLog(@"%@",filePath);
73 
74         [picker dismissViewControllerAnimated:YES completion:nil];
75         
76         [imagePathList addObject:filePath];
77 
78         
79         addImageView = [[UIImageView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(addImageView.frame), 0, SCREEN_WIDTH/3, SCREEN_WIDTH/3)];
80         addImageView.image = image;
81         addImageView.layer.borderWidth = 5;
82         addImageView.layer.borderColor = [UIColor whiteColor].CGColor;
83         [self.view addSubview:addImageView];
84         
85         CGSize size = [addressLabel.text sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]}];
86         addressLabel.frame = CGRectMake(10, CGRectGetMaxY(addImageView.frame), size.width+5, 20);
87         
88         inputView.frame = CGRectMake(0, CGRectGetMaxY(addressLabel.frame), SCREEN_WIDTH, SCREEN_HEIGHT-CGRectGetMaxY(addressLabel.frame));
89       
90     }
91 }
92 - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
93 {
94     [picker dismissViewControllerAnimated:YES completion:nil];
95 }
为了更好点交流和学习,请大家在参阅博客后,留下你的疑问和宝贵意见。谢谢!!!!
原文地址:https://www.cnblogs.com/fshmjl/p/4792146.html