image compression by size - iPhone SDK

Related searches

I would like to compress images (camera/photo library) and then send it to the server. I know I can compress by height and width, but I would like to compress the images by size to a fixed size (200 KB) only and keep the original height and width. The scale factor in JPEGRepresentation does not represent the size and only compression quality. How can I achieve this (compress to a fixed size) without using any third party library? Thanks for any help.

Heres some example code that will attempt to compress an image for you so that it doesn't exceed either a max compression or maximum file size

CGFloat compression = 0.9f;
CGFloat maxCompression = 0.1f;
int maxFileSize = 250*1024;

NSData *imageData = UIImageJPEGRepresentation(yourImage, compression);

while ([imageData length] > maxFileSize && compression > maxCompression)
{
    compression -= 0.1;
    imageData = UIImageJPEGRepresentation(yourImage, compression);
}

In Swift, how to reduce an image f…, Heres some example code that will attempt to compress an image for you so that it doesn't exceed either a max compression or maximum file size CGFloat� Tap the icon of a photo at the top left of the app screen (it looks like a mountain inside a box) and select the photo you want to resize from your Photos library. Now select the measurement format

One way to do it, is to re-compress the file in a loop, until you find the desired size. You could first find height and width, and guess the compression factor (larger image more compression) then after you compress it, check the size, and split the difference again.

I know this is not super efficient, but I do not believe there is a single call to achieve a image of a specific size.

HEIC Image Compression for iOS, - read the original image size. Then depending on its size : let roughSize = Int( sizeInBytes / 1_000_000). - if over 10 MB, apply the maximum compression you� Resizing. 3 Attempts Remaining Current Size: 844482 New Compressed Size: 357459 Ending Image Size: 2090332 As you can see the image started at 4mb big. It was compressed to 350kb, then in the end it was made into a UIImage as 2mb.

Here, JPEGRepresentation is quite memory consuming and if we use in Loop so it is extremely high memory consuming. So use below code & ImageSize won't be more then 200KB.

UIImage* newImage = [self captureView:yourUIView];


- (UIImage*)captureView:(UIView *)view {  
CGRect rect = view.bounds;
UIGraphicsBeginImageContext(rect.size);  
CGContextRef context = UIGraphicsGetCurrentContext();  

[view.layer renderInContext:context];  
UIImage* img = [UIImage alloc]init];
img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();  
NSLog(@"img=%@",img);
return img;
}

hucool/WXImageCompress: Image size quality compression, very , There are many data compression algorithms and there are no perfect one-size fits all solutions. For images, Apple has adopted the HEIC� compression d'image par taille - iPhone SDK Je voudrais compresser des images (caméra / photothèque), puis l'envoyer au serveur. Je sais que je peux compresser par hauteur et largeur, mais je voudrais compresser les images par taille à une taille fixe (200 Ko) seulement et garder la hauteur et la largeur d'origine.

I took the answer of @kgutteridge and made a similar solution for Swift 3.0 using recursive:

extension UIImage {
    static func compress(image: UIImage, maxFileSize: Int, compression: CGFloat = 1.0, maxCompression: CGFloat = 0.4) -> Data? {

        if let data = UIImageJPEGRepresentation(image, compression) {

            let bcf = ByteCountFormatter()
            bcf.allowedUnits = [.useMB] // optional: restricts the units to MB only
            bcf.countStyle = .file
            let string = bcf.string(fromByteCount: Int64(data.count))
            print("Data size is: \(string)")

            if data.count > (maxFileSize * 1024 * 1024) && (compression > maxCompression) {
                let newCompression = compression - 0.1
                let compressedData = self.compress(image: image, maxFileSize: maxFileSize, compression: newCompression, maxCompression: maxCompression)
                return compressedData
            }

            return data
        }

        return nil
    }
}

Whatsapp like image compression � GitHub, Image size quality compression, very close to WeChat picture compression strategy iOS 8.0+ | macOS 10.10+ | tvOS 9.0+ | watchOS 2.0+; Xcode 8� But I found this actually creates an image file that's larger in file size than the original, somewhat significantly larger even. I noticed in Apple's Sample Photo Editing Extension app they always set the quality to 0.9. I tried that and it does output a file size close to the original photo file size. This doesn't make sense to me.

After doing a few tests, I was able to find A relationship between image size and compression value. Since this relationship is linear for all values where the compression is less than 1, I created an algorithm to try to always compress images to a certain value.

//Use 0.99 because at 1, the relationship between the compression and the file size is not linear
NSData *image = UIImageJPEGRepresentation(currentImage, 0.99);
float maxFileSize = MAX_IMAGE_SIZE * 1024;

//If the image is bigger than the max file size, try to bring it down to the max file size
if ([image length] > maxFileSize) {
    image = UIImageJPEGRepresentation(currentImage, maxFileSize/[image length]);
}

whatsapp-image-compression. - (UIImage *)compressImage:(UIImage *)image{. float actualHeight = image.size.height;. float actualWidth = image.size.width;. That’s why we need ways to reduce video size when using iOS. Apple’s move to the new HEIF format is a big step in addressing the size problem. But at the moment the compatibility rate to the format is still low. So we still have to rely on the old-fashioned way to compress video size. Here are some ways to reduce video size when using iOS.

I want to reduce the size of gif images with the convert command. Currently I use the flags -depth 8, -type Grayscale, -depth 8 and -resize '400x300>'.. I also make sure to strip the first frame of any animated gifs with [0] after the source image file name.

I am using PIL to resize the images there by converting larger images to smaller ones. Are there any standard ways to reduce the file size of the image without losing the quality too much, lets say the original size of the image is 100KB, i want to get it down to like 5 or 10 KB especially for png and jpeg formats.

Some of those images are pretty big in size (widths larger than 500 pixels, for instance). Since the iPhone doesn't even have a big enough display to show the image in its original size, I'm planning on resizing the image to something a bit smaller to save on space/performance.

Comments
  • There is no API for it in the SDK. As you found out, you can either resize by resolution or by quality. We had the same problem and eventually we decided to resize to the screen display and compress by 85% which reduces the file-size dramatically but yet provides a very good image quality.
  • The above written code works great:). But I want my image to compress upto 20 kb, I tried to play with your code. But only able to get the size around 200 kb, can you guide me a way how can I achieve 20 kb.
  • @DeepK What above solution does is compresses the original Image. The max compression might not get you the expecting solution. So create the image from the compressed data and then again compress the image until you get what you want. just an Idea.
  • @Codetrix Thank you for suggesting the solution.
  • @kgutteidge is this code working by converting in to swift....i got an error on while condition...thats why i wanted to ask you
  • This answer is not correct and misleading. Please consider removing it
  • It's not safe to use while loop for such a heavy task!
  • The question is asking specifically for file size, not image dimensions