iphone - 如何在UIImagepickercontroller中获取URL图像

标签 iphone ios objective-c url uiimagepickercontroller

我想在拍照后在 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];

但是在控制台打印(null)

你能告诉我吗?谢谢

最佳答案

图像选择器有两个成功委托(delegate)方法,如下所示:

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

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

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

要访问 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];
}

希望这对你有帮助:)

关于iphone - 如何在UIImagepickercontroller中获取URL图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17273446/

相关文章:

iphone - 从 iOS 设备读取蓝牙 MAC 地址

iphone - 如何确定外接耳机是否已连接到 iPhone?

ios - 如何设置连续30天的UILocalNotification

ios - UICollectionView.backgroundView 坏了吗

ios - UIImagePickerController 错误

iphone - 如何检查本地 wifi 连接

iphone - 如何旋转 UIImage 以在网站/Facebook 上以正确的方向发布照片?

ios - Swift - 带有两行文本的 UIButton

iphone - Facebook IOS SDK 试图制作单例类?

objective-c - AFNetworking 如何检查上传操作是否完成或失败