如何在UIImagepickercontroller中获取URL图像

时间:2022-04-24 21:23:37

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

我想在拍照后在UIImagepickercontroller中获取URL图像。我在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];

But in console print (null)

但在控制台打印(null)

Can you show me ? Thanks

能给我看看么 ?谢谢

1 个解决方案

#1


1  

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.

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

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

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

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

Hope this helps you :)

希望这可以帮到你:)

#1


1  

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.

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

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

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

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

Hope this helps you :)

希望这可以帮到你:)