我正在尝试将所选图像保存到目录中。我的代码如下:
-(IBAction)attachPhotosButton
{
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:imagePicker animated:YES];
// If image is selected, then save
//[self saveImage];
// else do nothing
}
-(void)saveImage
{
// This creates a string with the path to the app's Documents Directory.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
// Here we are calling the method we created before to provide us with a unique file name.
NSString *imagePath = [NSString stringWithFormat:@"%@/iPhoneImage.png", documentsDirectory];
// This creates PNG NSData for the UIImageView imageView's image.
UIImageView *imageView;
NSData *imageData = UIImagePNGRepresentation(imageView.image);
// Uncomment the line below to view the image's file path in the Debugger Console. (Command+Shift+R)
NSLog(@"Image Directory: %@", imagePath);
// This actually creates the image at the file path specified, with the image's NSData.
[[NSFileManager defaultManager] createFileAtPath:imagePath contents:imageData attributes:nil];
}
如何在显示imagePicker视图控制器时检测到用户选择了图像?
如果他们选择了我想调用saveImage方法的图像。
此致 斯蒂芬
答案 0 :(得分:2)
您需要访问UIImagePickerControllerDelegate方法。该链接将您带到您想要的那个。确保设置了选择器委托!
在方法内部,您只需要检查信息字典的UIImagePickerControllerMediaType键是否设置为kUTTypeImage。然后打电话给你的功能。
答案 1 :(得分:1)
-(void) imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
UIImagePickerControllerDelegate的委托方法。
编辑:重新阅读您的问题并意识到您希望保存到临时路径或某个地方,而不是手机的相册。