2017-02-13 28 views
1

我想從iPad的圖片庫獲取圖片網址。圖片庫圖片URL「assets-library://asset/asset.JPG?id = 1000000007&ext = JPG」爲零

當我試圖讓UIImagePickerControllerReferenceURL從圖像信息Piicker
我收到網址爲:

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

讓我知道,如果我做錯了什麼。

+0

如果沒有保存,你還沒有網域。 – user3344236

回答

1

後iOS8,您應該使用Photos.framework訪問系統照片庫。

單張照片模型是PHAsset實例對象。它有pixelWidthpixelHeight屬性存儲當前照片的尺寸信息。通過這些信息,您可以計算其內存大小。

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