Get size of a UIImage (bytes length) not height and width

后端 未结 10 1974
说谎
说谎 2020-11-28 09:00

I\'m trying to get the length of a UIImage. Not the width or height of the image, but the size of the data.

相关标签:
10条回答
  • 2020-11-28 09:46
    - (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)
  • 2020-11-28 09:50

    Use the CGImage property of UIImage. Then using a combination of CGImageGetBytesPerRow *
    CGImageGetHeight, add in the sizeof UIImage, you should be within a few bytes of the actual size.

    This will return the size of the image, uncompressed, if you want to use it for purposes such as malloc in preparation for bitmap manipulation (assuming a 4 byte pixel format of 3 bytes for RGB and 1 for Alpha):

    int height = image.size.height,
        width = image.size.width;
    int bytesPerRow = 4*width;
    if (bytesPerRow % 16)
        bytesPerRow = ((bytesPerRow / 16) + 1) * 16;
    int dataSize = height*bytesPerRow;
    
    0 讨论(0)
  • 2020-11-28 09:51

    I'm not sure your situation. If you need the actual byte size, I don't think you do that. You can use UIImagePNGRepresentation or UIImageJPEGRepresentation to get an NSData object of compressed data of the image.

    I think you want to get the actual size of uncompressed image(pixels data). You need to convert UIImage* or CGImageRef to raw data. This is an example of converting UIImage to IplImage(from OpenCV). You just need to allocate enough memory and pass the pointer to CGBitmapContextCreate's first arg.

    UIImage *image = //Your image
    CGImageRef imageRef = image.CGImage;
    
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    IplImage *iplimage = cvCreateImage(cvSize(image.size.width, image.size.height), IPL_DEPTH_8U, 4);
    CGContextRef contextRef = CGBitmapContextCreate(iplimage->imageData, iplimage->width, iplimage->height,
                                                    iplimage->depth, iplimage->widthStep,
                                                    colorSpace, kCGImageAlphaPremultipliedLast|kCGBitmapByteOrderDefault);
    CGContextDrawImage(contextRef, CGRectMake(0, 0, image.size.width, image.size.height), imageRef);
    CGContextRelease(contextRef);
    CGColorSpaceRelease(colorSpace);
    
    IplImage *ret = cvCreateImage(cvGetSize(iplimage), IPL_DEPTH_8U, 3);
    cvCvtColor(iplimage, ret, CV_RGBA2BGR);
    cvReleaseImage(&iplimage);
    
    0 讨论(0)
  • 2020-11-28 09:54
     UIImage *img = [UIImage imageNamed:@"sample.png"];
     NSData *imgData = UIImageJPEGRepresentation(img, 1.0); 
     NSLog(@"Size of Image(bytes):%d",[imgData length]);
    
    0 讨论(0)
提交回复
热议问题