How to get the compressed file size of an image received from `UIImagePickerController`?

后端 未结 4 1448
再見小時候
再見小時候 2021-01-14 11:58

I want to know the size of image taken by UIImagePickerController by camara or library. Is there any way to find that ?

Requirement is like,

If

4条回答
  •  北荒
    北荒 (楼主)
    2021-01-14 12:14

    try this:

    CGFloat compression = 0.8f;
    CGFloat maxCompression = 0.1f;
    int maxFileSize = 1024;
    UIImage *img =[UIImage imageNamed:@"anyimg.jpg"];
    NSData *imageData = UIImageJPEGRepresentation(img, compression);
    
    while ([imageData length] > maxFileSize && compression > maxCompression)
    {
        compression -= 0.1;
        imageData = UIImageJPEGRepresentation(img, compression);
        NSLog(@"%d",[imageData length]);
    }
    NSLog(@"image size %d",[imageData length]);
    

    For get image size:
    NSData *data_size = UIImagePNGRepresentation(yourImage);
    int image_size =[data_size length];

提交回复
热议问题