2
votes

when i download a image use sdwebimage, and i print in the image downloader operation's - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {

        UIImage *image = [UIImage sd_imageWithData:self.imageData];
        NSString *key = [[SDWebImageManager sharedManager] cacheKeyForURL:self.request.URL];
        image = [self scaledImageForKey:key image:image];
        NSLog(@"didCompleteWithError:%@ data lenght:%ld png::%ld,jpg:%ld",self.request.URL,[self.imageData length],[UIImagePNGRepresentation(image) length],[UIImageJPEGRepresentation(image, 1) length]);

the logs turns that

data lenght:163480 png::202498,jpg:131774

and the raw data length is the exact file size as the image file in server , but when i create a image with these raw data , and use UIImagePNGRepresentation or UIImageJPEGRepresentation to get the UIImage's NSData length , it seems that both png and jpg presentation (no compression) of UIImage will be more larger the the raw data. what makes this happen?

1

1 Answers

0
votes

PNG Files

PNG format is a lossless compression file format, which makes it a common choice for use on the Web. PNG is a good choice for storing line drawings, text, and iconic graphics at a small file size.

Portable Network Graphics is a lossless file format created with the intent to replace the GIF format, due to the patent restrictions of GIF compression. The project was a success and we now have complete access to the format, which is patent-free, has great compression, and is widely supported by web browsers. PNG files are used primarily for transparent images, simple-color images, and images that have hard lines, like text. There are two versions of PNG files: 8-bit PNG(known as PNG-8) and 24-bit PNG(known as PNG-24). PNG-8 is limited to 256 indexed colors, while PNG-24 has millions.

JPEG Files

Joint Photographic Experts Group created a file format, creatively named JPEG \ˈjā-ˌpeg\, to handle complex-color photographic images. When saving a file as a JPEG, users have the choice of quality vs. compression. More compression results in a smaller file size, but you will lose quality. Obviously, less compression results in a larger file-size, but also a higher-quality image. The great thing about JPEG compression is that you can usually find a balance that both looks good and has a small file size. Unfortunately, JPEG files have no transparency. Additionally, the file format is lossy, meaning that it loses some of it’s data each time it is compressed. If you re-save the same image multiple times for some reason, the image quality may be low.

JPG format is a lossy compressed file format. This makes it useful for storing photographs at a smaller size than a BMP. JPG is a common choice for use on the Web because it is compressed. For storing line drawings, text, and iconic graphics at a smaller file size, GIF or PNG are better choices because they are lossless.

JPEGs are for photographs and realistic images. PNGs are for line art, text-heavy images, and images with few colors. GIFs are just fail.