3

I need to read the properties of an image without load or download it. In fact i have implemented a simple method that use the CGImageSourceCreateWithUrl for accomplish this. My problem is that it is returning always error because seems that the imageSource is null. So what can i do for fix it? In the NSURL object i pass urls like: "http://www.example.com/wp-content/uploads/image.jpg" but also ALAssets library Id used to retrieve images inside the phone like "assets-library://asset/asset.JPG?id=E5F41458-962D-47DD-B5EF-E606E2A8AC7A&ext=JPG". This is my method:

-(NSString *) getPhotoInfo:(NSString *)paths{
  NSString *xmlList = @“test”;

  NSURL * imageFileURL = [NSURL fileURLWithPath:paths];
  NSLog(@"imageFileURL %@", imageFileURL);
  CGImageSourceRef imageSource = CGImageSourceCreateWithURL((__bridge CFURLRef)(imageFileURL), NULL);
  if (imageSource == NULL) {
    // Error loading image
    NSLog(@"Error loading image");
  }
  CGFloat width = 0.0f, height = 0.0f;
  CFDictionaryRef imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, NULL);
  NSLog(@"image source %@", imageSource);

  return xmlList;
}

I have saw this posts for try to fix it but nothing seems working:

In my project ARC is enabled.

Thanks

Community
  • 1
  • 1
Hieicker
  • 595
  • 1
  • 11
  • 29

2 Answers2

5

If you are passing the string "http://www.example.com/wp-content/uploads/image.jpg” to -fileURLWithPath: it’s going to return nil, because that string is sure not a file path, it’s a URL string.

Think of -fileURLWithPath: as just prepending the string you pass in with “file://localhost/“...so you’d end up with a URL that looks like "file://localhost/http://www.example.com/wp-content/uploads/image.jpg”. That’s not good.

You need to call [NSURL URLWithString:paths] if you’re going to be passing in entire URL strings, not just a filesystem path strings.

Wil Shipley
  • 9,343
  • 35
  • 59
  • Thanks for the explanation. It is more clear now if i use an url like "http://example.com/name.jpg". But what change in the case that i use an internal path like "assets-library://asset/asset.JPG?id=E5F41458-962D-47DD-B5EF-E606E2A8AC7A&ext=JPG" ? It is founded using the ALAsset library. In this case it return always null. – Hieicker Jan 31 '14 at 10:57
  • [NSURL URLWithString:] should work with "assets-library://asset/asset.JPG?id=E5F41458-962D-47DD-B5EF-E606E2A8AC7A&ext=JP‌​G”, I would think. It returns nil, you’re saying? – Wil Shipley Jan 31 '14 at 11:06
  • Thanks! In fact actually it isn't returning nil but this kind of error "ImageIO: CGImageSourceCreateWithURL CFURLCreateDataAndPropertiesFromResource failed with error code -11." It returns also the error "Error loading image" and obviously after it return (always in the log) imageSource (null) – Hieicker Jan 31 '14 at 11:12
3

Working with "assets-library://asset/asset.JPG?id.........., try this code

-(UIImage*)resizeImageToMaxSize:(CGFloat)max anImage:(UIImage*)anImage  
{

     NSData * imgData = UIImageJPEGRepresentation(anImage, 1);
 CGImageSourceRef imageSource = CGImageSourceCreateWithData((CFDataRef)imgData, NULL);
 if (!imageSource)
     return nil;

 CFDictionaryRef options = (CFDictionaryRef)[NSDictionary dictionaryWithObjectsAndKeys:
                                             (id)kCFBooleanTrue, (id)kCGImageSourceCreateThumbnailWithTransform,
                                             (id)kCFBooleanTrue, (id)kCGImageSourceCreateThumbnailFromImageIfAbsent,
                                             (id)[NSNumber numberWithFloat:max], (id)kCGImageSourceThumbnailMaxPixelSize,
                                             nil];
 CGImageRef imgRef = CGImageSourceCreateThumbnailAtIndex(imageSource, 0, options);

 UIImage* scaled = [UIImage imageWithCGImage:imgRef];
  //these lines might be skipped for ARC enabled
 CGImageRelease(imgRef);
 CFRelease(imageSource);

 return scaled; }
Antzi
  • 12,831
  • 7
  • 48
  • 74
Usman Nisar
  • 3,031
  • 33
  • 41