How to get Size of UIImage in Bytes in iOS 4.0+?

后端 未结 4 1115
北恋
北恋 2021-02-08 08:01

I am trying to pick an image from the photo library or from the camera.
The delegate method:

- (void)imagePickerController:(UIImagePickerController *)picker d         


        
相关标签:
4条回答
  • 2021-02-08 08:14

    Try the following code:

    NSData *imageData = [[NSData alloc] initWithData:UIImageJPEGRepresentation((image), 1.0)];
    
    int imageSize = imageData.length;
    NSLog(@"SIZE OF IMAGE: %i ", imageSize);
    
    0 讨论(0)
  • 2021-02-08 08:24

    I know this is an old question but creating a NSData object just to get the byte-size of an image can be a really expensive operation. Image can have over 20Mb and creating equally sized object just to get the size of the first one...

    I tend to use this category:

    UIImage+CalculatedSize.h

    #import <UIKit/UIKit.h>
    
    @interface UIImage (CalculatedSize)
    
    -(NSUInteger)calculatedSize;
    
    @end
    

    UIImage+CalculatedSize.m

    #import "UIImage+CalculatedSize.h"
    
    @implementation UIImage (CalculatedSize)
    
    -(NSUInteger)calculatedSize
    {    
        return CGImageGetHeight(self.CGImage) * CGImageGetBytesPerRow(self.CGImage);
    }
    
    @end
    

    You simply import the UIImage+CalculatedSize.h and use it like this:

    NSLog (@"myImage size is: %u",myImage.calculatedSize);
    

    Or, if you want to avoid using categories:

    NSUInteger imgSize  = CGImageGetHeight(anImage.CGImage) * CGImageGetBytesPerRow(anImage.CGImage);
    

    EDIT:

    This calculation of course has nothing to do with JPEG/PNG compression. It relates to underlaying CGimage:

    A bitmap (or sampled) image is a rectangular array of pixels, with each pixel representing a single sample or data point in a source image.

    In a way a size retrieved this way gives you a worst-case scenario information without actually creating an expensive additional object.

    0 讨论(0)
  • 2021-02-08 08:28

    From:@fbrereto's answer:

    The underlying data of a UIImage can vary, so for the same "image" one can have varying sizes of data. One thing you can do is use UIImagePNGRepresentation or UIImageJPEGRepresentation to get the equivalent NSData constructs for either, then check the size of that.

    From:@Meet's answer:

     UIImage *img = [UIImage imageNamed:@"sample.png"];
     NSData *imgData = UIImageJPEGRepresentation(img, 1.0); 
     NSLog(@"Size of Image(bytes):%d",[imgData length]);
    
    0 讨论(0)
  • 2021-02-08 08:38
    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)editInfo{
       UIImage *image=[editInfo valueForKey:UIImagePickerControllerOriginalImage];
       NSURL *imageURL=[editInfo valueForKey:UIImagePickerControllerReferenceURL];
       __block long long realSize;
    
       ALAssetsLibraryAssetForURLResultBlock resultBlock=^(ALAsset *asset)
       {
          ALAssetRepresentation *representation=[asset defaultRepresentation];
          realSize=[representation size];
       };
    
       ALAssetsLibraryAccessFailureBlock failureBlock=^(NSError *error)
       {
          NSLog(@"%@", [error localizedDescription]);
       };
    
       if(imageURL)
       {
          ALAssetsLibrary *assetsLibrary=[[[ALAssetsLibrary alloc] init] autorelease];
          [assetsLibrary assetForURL:imageURL resultBlock:resultBlock failureBlock:failureBlock];
       }
    }
    
    0 讨论(0)
提交回复
热议问题