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];
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];
相关文章推荐
- Facebook开源的基于SQL的操作系统检测和监控框架:osquery
- VS2008 LINK : fatal error LNK1000: Internal error during IncrBuildImage
- 拍照图片方向
- IOS系列-UIProgressView控件用法
- 通用查询功能第三版EasyQueryVer3使用说明
- iOS开发系列--UISlider用法总结
- 转发(request.setRequestDispacter)和重定向(response.sendRedirect)的区别以及转发的include和forward的区别
- request和response的相关
- 体会建造者模式Builder
- Codeforces 245H Queries for Number of Palindromes
- 高效读写的队列:深度剖析ConcurrentLinkedQueue
- Android UI线程和非UI线程
- iOS将UIImage设置为圆角
- request中的方法小结
- UINavigationController、UITabBarController
- 导致UIButton半透明的原因
- UITextField 全属性
- Sprite (2D and UI)
- Android 更新UI的两种方式--Handler & runOnUiThread
- android进行异步更新UI的四种方式