Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CGImageSourceCreateWithURL returns always NULL

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:

  • CGImageSourceRef imageSource = CGImageSourceCreateWithURL returns NULL
  • CGImageSourceCreateWithURL with authentication
  • accessing UIImage properties without loading in memory the image

In my project ARC is enabled.

Thanks

like image 321
Hieicker Avatar asked Apr 02 '26 00:04

Hieicker


1 Answers

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.

like image 126
Wil Shipley Avatar answered Apr 04 '26 15:04

Wil Shipley



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!