oc获得相册照片

时间:2022-07-14 08:39:20
 - (void)addImage
{
if (CGRectGetMaxX(addImageView.frame)>SCREEN_WIDTH-CGRectGetWidth(addImageView.frame)) {
[self ShowHUDTitle:@"只能添加三张图片" andDelay:];
return;
}
UIActionSheet *actionsheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles: @"打开照相机", @"从手机相册获取",nil]; [actionsheet showInView:self.view];
} - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == actionSheet.cancelButtonIndex) {
}
switch (buttonIndex) {
case ://打开照相机
[self takePhoto];
break;
case ://打开相册
[self localPhoto];
break;
default:
break;
}
} -(void)takePhoto//打开相机
{
UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIImagePickerController *picker = [[UIImagePickerController alloc]init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = sourceType;
[self presentViewController:picker animated:YES completion:nil];
}
}
-(void)localPhoto//本地相册
{
UIImagePickerController *picker = [[UIImagePickerController alloc]init];
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.delegate = self;
picker.allowsEditing = YES;
[self presentViewController:picker animated:YES completion:nil];
} -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
// 当选择的类型是图片
if ([type isEqualToString:@"public.image"]) {
// 把图片转化为NSData
UIImage *image = info[UIImagePickerControllerEditedImage];
NSData *data = UIImageJPEGRepresentation(image, 0.03); // 图片的保存路径
NSString *documentsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSFileManager *manager = [NSFileManager defaultManager]; // 将刚刚转化的图片放到沙盒中 并保存为png
[manager createDirectoryAtPath:documentsPath withIntermediateDirectories:YES attributes:nil error:nil];
NSString *imageName = [NSString stringWithFormat:@"/%@.png",[NSDate date]]; [manager createFileAtPath:[documentsPath stringByAppendingString:[NSString stringWithFormat:@"%@",imageName]] contents:data attributes:nil]; //得到选择后沙盒的路径
filePath = [[NSString alloc]initWithFormat:@"%@%@",documentsPath,imageName];
NSLog(@"%@",filePath); [picker dismissViewControllerAnimated:YES completion:nil]; [imagePathList addObject:filePath]; addImageView = [[UIImageView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(addImageView.frame), , SCREEN_WIDTH/, SCREEN_WIDTH/)];
addImageView.image = image;
addImageView.layer.borderWidth = ;
addImageView.layer.borderColor = [UIColor whiteColor].CGColor;
[self.view addSubview:addImageView]; CGSize size = [addressLabel.text sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:]}];
addressLabel.frame = CGRectMake(, CGRectGetMaxY(addImageView.frame), size.width+, ); inputView.frame = CGRectMake(, CGRectGetMaxY(addressLabel.frame), SCREEN_WIDTH, SCREEN_HEIGHT-CGRectGetMaxY(addressLabel.frame)); }
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissViewControllerAnimated:YES completion:nil];
}