最近做个项目用到这个,先保存起来备用
//////////////////////////////// .h 文件
@interface ViewController :UIViewController<UIActionSheetDelegate,
UINavigationControllerDelegate,
UIImagePickerControllerDelegate> {
}
/////////////////////////// .m文件
-(IBAction)scanning:(id)sender {
UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:@"保存图片"
delegate:self
cancelButtonTitle:@"取消"
destructiveButtonTitle:@"保存到相册"
otherButtonTitles:@"获取图片",nil];
actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
[actionSheet showInView:self.view];
}
#pragma Mark UIImagePickerControllerDelegate
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0) {
if (nil != _video.currentImage) {
#if 0
//往照片库里面存图片时,经常发生缩略图能看到但原图消失的问题
UIImageWriteToSavedPhotosAlbum(_video.currentImage, nil, nil, nil);
#else
[self saveMImage:_video.currentImage];
#endif
}
else {
NSLog(@"保存不成功.............");
}
}
else if(buttonIndex == 1) { //获取图片
NSUInteger sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
// 判断是否支持相机
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
switch (buttonIndex) {
case 0: //相机
sourceType = UIImagePickerControllerSourceTypeCamera;
break;
case 1: //相册
sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
break;
}
}
else {
sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
}
// 跳转到相机或相册页面
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.delegate = self;
imagePickerController.allowsEditing = YES;
imagePickerController.sourceType = sourceType;
[self presentViewController:imagePickerController animated:YES completion:^{}];
}
else if(buttonIndex == 2) { //取消
}
}
#pragma mark - image picker delegte
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[picker dismissViewControllerAnimated:YES completion:^{}];
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
NSLog(@"get Image ok...%@ ..............",image);
}
-(void)saveMImage:(UIImage *)aImage
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:@"savedImage.png"];
//also be .jpg or another
UIImage *image = aImage; // imageView is your image
// Returns the data for the specified image in JPEG/PNG format.
NSData *imageData = UIImagePNGRepresentation(image);
//UIImageJPEGRepresentation(image)
[imageData writeToFile:savedImagePath atomically:NO];
}