简体   繁体   中英

how to get the image size in ios app coding

I have retrieved an image from the imageGallery. Now I want to know about the filesize of the image, in KB. How do I do this? Please guide me. Is it possible to find it?

yes you can get the size. Try the following code:

NSData *imgData = [[NSData alloc] initWithData:UIImageJPEGRepresentation((image), 0.5)];
int imageSize   = imgData.length;
NSLog(@"size of image in KB: %f ", imageSize/1024.0);
    BOOL isPNG = [[yourImgPath lowercaseString] isEqualToString:@"png"];

    NSData *imageData = isPNG ?
    UIImagePNGRepresentation(img) :
    UIImageJPEGRepresentation(img, 0);

    int imageSize   = imageData.length;
    NSLog(@"size of image in KB: %f ", imageSize/1024.0);

Try this one:

NSString *imagePath=[[NSBundle mainBundle]pathForResource:@"images" ofType:@"jpeg"];
UIImage *image=[[UIImage alloc]initWithContentsOfFile:imagePath];

NSError *error;
NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:imagePath error:&error];
NSNumber *fileSizeNumber = [fileAttributes objectForKey:NSFileSize];
long long fileSize = [fileSizeNumber longLongValue];
NSLog(@"size of image is : %fKB ", fileSize/1024.0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM