照片库图像URL“assets-library://asset/asset.JPG?id = 1000000007& ext = JPG”为零

时间:2017-02-13 06:22:06

标签: ios objective-c ipad

我想从iPad的照片库中获取图片网址。

当我想从Image Piicker的信息中获取 UIImagePickerControllerReferenceURL 时 我得到的URL为:

 assets-library://asset/asset.JPG?id=1000000007&ext=JPG

因为我希望在不加载到内存中的情况下获取图像元数据(image height,width and size in MB)

我尝试了以下代码:

 -(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
    {
        NSURL *mediaURL;
        mediaURL=(NSURL*)[info valueForKey:UIImagePickerControllerMediaURL];
        NSURL *imageFileURL = (NSURL*)[info valueForKey:UIImagePickerControllerReferenceURL];

        NSLog(@" referenURL %@  mediaURL %@" ,imageFileURL,mediaURL);

        //We can get Image property from imagepath.
        //NSURL *imageFileURL = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@",_directoryPath,roomImageNames[i]]];
        CGImageSourceRef imageSource = CGImageSourceCreateWithURL((__bridge CFURLRef)imageFileURL, NULL);
        NSDictionary *properties = (__bridge_transfer NSDictionary *)CGImageSourceCopyPropertiesAtIndex(imageSource, 0, NULL);

        CGFloat height = [[properties objectForKey:@"PixelHeight"] floatValue];
        CGFloat width = [[properties objectForKey:@"PixelWidth"] floatValue];
        NSLog(@"height %f  width  %f",height ,width);
}

我的图像高度和宽度为0.0

如果我做错了,请告诉我。

2 个答案:

答案 0 :(得分:1)

这些功能允许您访问某些图像元数据,而无需将实际像素数据加载到内存中。例如,获取像素尺寸就像这样(确保在目标中包含 ImageIO.framework ):

#import <ImageIO/ImageIO.h>

NSURL *imageFileURL = [NSURL fileURLWithPath:...];
CGImageSourceRef imageSource = CGImageSourceCreateWithURL((CFURLRef)imageFileURL, NULL);
if (imageSource == NULL) {
    // Error loading image
    ...
    return;
}

NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                         [NSNumber numberWithBool:NO], (NSString *)kCGImageSourceShouldCache,
                         nil];
CFDictionaryRef imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, (CFDictionaryRef)options);
if (imageProperties) {
    NSNumber *width = (NSNumber *)CFDictionaryGetValue(imageProperties, kCGImagePropertyPixelWidth);
    NSNumber *height = (NSNumber *)CFDictionaryGetValue(imageProperties, kCGImagePropertyPixelHeight);
    NSLog(@"Image dimensions: %@ x %@ px", width, height);
    CFRelease(imageProperties);
}
CFRelease(imageSource);

有关详情:Accessing Image Properties Without Loading the Image Into Memory

答案 1 :(得分:1)

在iOS8之后,您应该使用Photos.framework访问系统照片库。

单张照片模型是PHAsset实例对象。它具有pixelWidthpixelHeight属性,用于存储当前照片的大小信息。通过这些信息,您可以计算其内存大小。