UIImagePickerController - 保存选定的图像

发布于 2024-09-16 17:24:59 字数 1471 浏览 3 评论 0原文

我正在尝试将选定的图像保存到目录中。我的代码如下:

-(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 方法。

问候, 斯蒂芬

I'm trying to save a selected image to a directory. My code is as follows:

-(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];
}

How do I detect that the user selected an image when the imagePicker view controller is presented ?

If they select an image I want to call the saveImage method.

Regards,
Stephen

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浮光之海 2024-09-23 17:24:59

您需要访问 UIImagePickerControllerDelegate 方法。该链接会将您带到您想要的链接。确保您设置了选择器委托!

在方法内部,您只需要检查信息字典的 UIImagePickerControllerMediaType 键是否设置为 kUTTypeImage。然后调用你的函数。

You need to access the UIImagePickerControllerDelegate methods. The link brings you to the one you want. Make sure that you set the picker delegate!

Inside the method you just need to check if the UIImagePickerControllerMediaType key of the info dictionary is set to a kUTTypeImage. Then call your function.

月下伊人醉 2024-09-23 17:24:59

-(void) imagePickerController:(UIImagePickerController *)picker 
        didFinishPickingMediaWithInfo:(NSDictionary *)info  

UIImagePickerControllerDelegate 的委托方法中。

编辑:重新阅读您的问题,并意识到您想要保存到临时路径或其他地方,而不是手机的相册。

in the

-(void) imagePickerController:(UIImagePickerController *)picker 
        didFinishPickingMediaWithInfo:(NSDictionary *)info  

delegate method of UIImagePickerControllerDelegate.

Edit: reread your question and realized you want to save to your temp path or somewhere, not the phone's photo album.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文