如何在 UIImagepickercontroller 中获取 URL 图片 [英] How to get URL image in UIImagepickercontroller

查看:36
本文介绍了如何在 UIImagepickercontroller 中获取 URL 图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在拍照后在 UIImagepickercontroller 中获取 URL 图片.我在 didFinishPickingMedia 中使用了以下代码..

I want to get URL image in UIImagepickercontroller after take picture. I used following codes in didFinishPickingMedia..

 NSData *webData = UIImagePNGRepresentation(image);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *localFilePath = [documentsDirectory stringByAppendingPathComponent:png];
[webData writeToFile:localFilePath atomically:YES];
NSLog(@"localFilePath.%@",localFilePath);

UIImage *image1 = [UIImage imageWithContentsOfFile:localFilePath];

但在控制台打印(null)

你能给我看看吗?谢谢

推荐答案

图片选择器有两种成功委托方法如下:

Image picker has two success delegate methods as follow:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo;
// This is Deprecated in ios 3.0

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

在我看来,您使用的是第一种方法(您没有提到该方法,但我想让您知道).在这种情况下,您必须将实现更改为第二种方法.

It seemed to me that you are using the first method (You didn't mentioned the method but I thought to let you know). In this case you have to change your implementation to the second method.

要访问 UIImage 并将其存储到某个路径,请按如下方式使用:

For accessing UIImage and storing it to some path use as follow:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    //Zoomed or scrolled image if picker.editing = YES;
    UIImage *editedImage = [info objectForKey:UIImagePickerControllerEditedImage]; 
    // Original Image
    UIImage *OriginalImage = [info objectForKey:UIImagePickerControllerOriginalImage]; 

    // You can directly use this image but in case you want to store it some where
    NSString *docDirPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *filePath =  [docDirPath stringByAppendingPathComponent:@"myImage.png"];
    NSLog (@"File Path = %@", filePath);

    // Get PNG data from following method
    NSData *myData =     UIImagePNGRepresentation(editedImage);
    // It is better to get JPEG data because jpeg data will store the location and other related information of image.
    [myData writeToFile:filePath atomically:YES];

    // Now you can use filePath as path of your image. For retrieving the image back from the path
    UIImage *imageFromFile = [UIImage imageWithContentsOfFile:filePath];
}

希望对你有帮助:)

这篇关于如何在 UIImagepickercontroller 中获取 URL 图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆