您的位置:首页 > 产品设计 > UI/UE

ios 调用系统相机,图库,相册。将照片保存在相册,保存图片在本地,UIImagePickerController

2016-06-28 16:36 941 查看
UIImagePickerController *imagePicker = [[UIImagePickerController
alloc]
init];

    imagePicker.allowsEditing =
YES;

    imagePicker.delegate =
self;

    imagePicker.sourceType =
UIImagePickerControllerSourceTypeSavedPhotosAlbum;

/*

typedef NS_ENUM(NSInteger, UIImagePickerControllerSourceType) {

    UIImagePickerControllerSourceTypePhotoLibrary,//图库

    UIImagePickerControllerSourceTypeCamera,//调用相机

    UIImagePickerControllerSourceTypeSavedPhotosAlbum //调用相册

} __TVOS_PROHIBITED;

*/

//用户点击图像选取器中的“cancel”按钮时被调用,这说明用户想要中止选取图像的操作

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker

{

    [picker dismissViewControllerAnimated:YES
completion:nil];

}

//点击选择要调用的方法;

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString
*,id> *)info

{

    NSDictionary *dic = info;

    UIImage *image = dic[UIImagePickerControllerOriginalImage];

    

    _imageView.image = image;

    [self
dismissViewControllerAnimated:YES
completion:nil];

    

    

}

#pragma mark-----------------------图片保存到相册;

- (void)saveImageToPhotos:(UIImage*)savedImage

{

    UIImageWriteToSavedPhotosAlbum(savedImage,
self, @selector(image:didFinishSavingWithError:contextInfo:),
NULL);

}

// 指定回调方法

- (void)image: (UIImage *) image didFinishSavingWithError: (NSError *)
error contextInfo: (void *) contextInfo

{

    NSString *msg =
nil ;

    if(error !=
NULL){

        msg = @"保存图片失败" ;

    }else{

        msg = @"保存图片成功" ;

    }

    UIAlertView *alert = [[UIAlertView
alloc]
initWithTitle:@"保存图片结果提示"

                                                    message:msg

                                                   delegate:self

                                          cancelButtonTitle:@"确定"

                                          otherButtonTitles:nil];

    [alert show];

}

-(void)saveImage:(UIImage *)image{

    NSLog(@"保存照片成功");

    

    

}

//保存图片到本地

        NSData *imageDate =
UIImageJPEGRepresentation(image,
0);

        NSString *fullPath = [[NSHomeDirectory()
stringByAppendingPathComponent:@"Document"]
stringByAppendingPathComponent:@"image"];

        NSLog(@"%@",fullPath);

        [imageDate writeToFile:fullPath
atomically:YES];

        image = [[UIImage
alloc] initWithContentsOfFile:fullPath];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: