Muhammad Zeeshan Anwar
Muhammad Zeeshan Anwar

Reputation: 146

How to get Image size from URL in ios

How can I get the size(height/width) of an image from URL in objective-C? I want my container size according to the image. I am using AFNetworking 3.0. I could use SDWebImage if it fulfills my requirement.

Upvotes: 6

Views: 13264

Answers (5)

Abdul Karim
Abdul Karim

Reputation: 4523

Swift 4.x
Xcode 12.x

func sizeOfImageAt(url: URL) -> CGSize? {
    // with CGImageSource we avoid loading the whole image into memory
    guard let source = CGImageSourceCreateWithURL(url as CFURL, nil) else {
        return nil
    }
    
    let propertiesOptions = [kCGImageSourceShouldCache: false] as CFDictionary
    guard let properties = CGImageSourceCopyPropertiesAtIndex(source, 0, propertiesOptions) as? [CFString: Any] else {
        return nil
    }
    
    if let width = properties[kCGImagePropertyPixelWidth] as? CGFloat,
       let height = properties[kCGImagePropertyPixelHeight] as? CGFloat {
        return CGSize(width: width, height: height)
    } else {
        return nil
    }
}

Upvotes: 6

Nike Kov
Nike Kov

Reputation: 13698

For Swift 4 use this:

let imageURL = URL(string: post.imageBigPath)!
let source = CGImageSourceCreateWithURL(imageURL as CFURL,     
let imageHeader = CGImageSourceCopyPropertiesAtIndex(source!, 0, nil)! as NSDictionary;
print("Image header: \(imageHeader)")

The header would looks like:

Image header: { ColorModel = RGB; Depth = 8; PixelHeight = 640; PixelWidth = 640; "{Exif}" = { PixelXDimension = 360; PixelYDimension = 360; }; "{JFIF}" = { DensityUnit = 0; JFIFVersion = ( 1, 0, 1 ); XDensity = 72; YDensity = 72; }; "{TIFF}" = { Orientation = 0; }; }

So u can get from it the Width, Height.

Upvotes: 1

Bohm
Bohm

Reputation: 1004

Knowing the size of an image before actually loading it can be necessary in a number of cases. For example, setting the height of a tableView cell in the heightForRowAtIndexPath method while loading the actual image later in the cellForRowAtIndexPath (this is a very frequent catch 22).

One simple way to do it, is to read the image header from the server URL using the Image I/O interface:

#import <ImageIO/ImageIO.h>

NSMutableString *imageURL = [NSMutableString stringWithFormat:@"http://www.myimageurl.com/image.png"];

CGImageSourceRef source = CGImageSourceCreateWithURL((CFURLRef)[NSURL URLWithString:imageURL], NULL);
NSDictionary* imageHeader = (__bridge NSDictionary*) CGImageSourceCopyPropertiesAtIndex(source, 0, NULL);
NSLog(@"Image header %@",imageHeader);
NSLog(@"PixelHeight %@",[imageHeader objectForKey:@"PixelHeight"]);

Upvotes: 19

Dipen Panchasara
Dipen Panchasara

Reputation: 13600

Use Asynchronous mechanism called GCD in iOS to dowload image without affecting your main thread.

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

    // Download IMAGE using URL
    NSData *data = [[NSData alloc]initWithContentsOfURL:URL];

    // COMPOSE IMAGE FROM NSData
    UIImage *image = [[UIImage alloc]initWithData:data];

    dispatch_async(dispatch_get_main_queue(), ^{
        // UI UPDATION ON MAIN THREAD
        // Calcualte height & width of image
        CGFloat height = image.size.height;
        CGFloat width = image.size.width;

    });
});

Upvotes: 3

TheHungryCub
TheHungryCub

Reputation: 1960

you can try like this:

NSData *data = [[NSData alloc]initWithContentsOfURL:URL]; 
UIImage *image = [[UIImage alloc]initWithData:data];
CGFloat height = image.size.height;
CGFloat width = image.size.width;

Upvotes: 0

Related Questions