iOS Crazy detailed ImageIO complete progressive loading picture

Source: Internet
Author: User

First, the common progressive loading picture mode

Currently we see the following three implementations of progressive loading:

1) load different sizes of pictures from the web, from small to large. First pull a small thumbnail to do the stretching display, and then pull the medium-sized diagram, pull over the direct overlay display, and finally pull the original image, pull completed after the display of the original.

2) pull the largest picture directly from the Web, and display a little bit of image every bit of data, so it will be refreshed from top to bottom.

3) combined with the 1th and 2nd, pull a thumbnail to do the stretching display, and then use the second method to pull the original image directly, so that the progressive loading can be achieved, but also save a few intermediate network requests.

Second, through the ImageIO to achieve the progressive loading of pictures

ImageIO's guide says this: "If you havea very large image, or is loading image data over the Web, your may want to create An incremental image of source so, can draw the image data as you accumulate it. "

"If you want to load a very large picture, or load a picture from the Web, you can achieve a progressive load by creating a imagesource," says the translator. "Translation is not very authentic, it is probably this meaning, in the past when doing Powercam, at that time, in order to deal with large images on iOS also tried this method, when the test used a map of China, the resolution of 10000*8000, the result is when the entire picture loaded into memory, Memory is too unbearable, so I gave up. Now think about this super-large picture processing, we can use the way of the Shard, each time only need to deal with a small piece of picture, this problem is left for everyone to think about it.

Today we are going to discuss the Cgimagesource implementation from the Web side of the gradual loading of images, to achieve this goal we need to create a urlconnnection, and then implement the proxy, each time the data received when the image is updated. The following main implementation of the source code:

////SVINCREMENTALLYIMAGE.M//Svincrementallyimage////Created by Maple on 6/27/13.//Copyright (c) Maple. All rights reserved.//#import "SvIncrementallyImage.h"#import<ImageIO/ImageIO.h>#import<CoreFoundation/CoreFoundation.h>@interfaceSvincrementallyimage () {nsurlrequest*_request; Nsurlconnection*_conn;        Cgimagesourceref _incrementallyimgsource; Nsmutabledata*_recievedata; Long Long_expectedleght; BOOL_isloadfinished;} @property (nonatomic, retain) UIImage*image; @property (nonatomic, retain) UIImage*Thumbimage;@end@implementationSvincrementallyimage@synthesizeImageURL =_imageurl;@synthesizeImage =_image;@synthesizeThumbimage =_thumbimage;- (ID) Initwithurl: (Nsurl *) imageurl{ Self=[Super Init]; if(self) {_imageurl=[ImageURL retain]; _request=[[Nsurlrequest alloc] initwithurl:_imageurl]; _conn= [[Nsurlconnection alloc] Initwithrequest:_requestDelegate: Self]; _incrementallyimgsource=cgimagesourcecreateincremental (NULL); _recievedata=[[Nsmutabledata alloc] init]; _isloadfinished=false; }        returnSelf ;}#pragmaMark-#pragmaMark Nsurlconnectiondatadelegate-(void) Connection: (Nsurlconnection *) connection didreceiveresponse: (Nsurlresponse *) response{_expectedleght=response.expectedcontentlength; NSLog (@"expected Length:%lld", _expectedleght); NSString*mimetype =Response.    MIMEType; NSLog (@"MIME TYPE%@", MimeType); Nsarray*arr = [MimeType componentsseparatedbystring:@"/"]; if(Arr.count <1|| ! [Arr Objectatindex:0] IsEqual:@"Image"]) {NSLog (@"Not a image URL");        [Connection Cancel]; [_conn release]; _conn=Nil; }}- (void) Connection: (Nsurlconnection *) connection didfailwitherror: (Nserror *) error{NSLog (@"Connection%@ Error, error info:%@", connection, error);}- (void) Connectiondidfinishloading: (Nsurlconnection *) connection{NSLog (@"Connection Loading finished!!!"); //If download image data not complete, create final image    if(!_isloadfinished)        {Cgimagesourceupdatedata (_incrementallyimgsource, (cfdataref) _recievedata, _isloadfinished); Cgimageref Imageref= Cgimagesourcecreateimageatindex (_incrementallyimgsource,0, NULL); Self.image=[UIImage Imagewithcgimage:imageref];    Cgimagerelease (IMAGEREF); }}- (void) Connection: (Nsurlconnection *) connection didreceivedata: (NSData *) data{[_recievedata Appenddata:data]; _isloadfinished=false; if(_expectedleght = =_recievedata.length) {_isloadfinished=true;    } cgimagesourceupdatedata (_incrementallyimgsource, (cfdataref) _recievedata, _isloadfinished); Cgimageref Imageref= Cgimagesourcecreateimageatindex (_incrementallyimgsource,0, NULL); Self.image=[UIImage Imagewithcgimage:imageref]; Cgimagerelease (IMAGEREF);}@end

From the above code we can see that at first we create a urlconnection based on the incoming URL, and create an empty Cgimagesource, Then call Cgimagesourceupdatedata to update imagesource data each time you receive the data, and then call Cgimagesourcecreateimageatindex to get the latest image.

How to see the above implementation is not feel to realize the gradual loading of images from the web is very simple, although imageio to help us do a lot of things, but we should also understand its principles. We know that the files are formatted, and the head of the general file records some data about the file format, followed by the actual file data.

Take the simplest BMP image file for example:

1) at the beginning of the Bitmapfileheader, this section mainly records the size of the file, as well as the actual distance from the file header of the image data.

2) followed by Bitmapinfoheader, this part of the main record picture of the width, height, bit depth and other information

3) Optional Palette information

4) The final part is the actual picture data.

The first three parts of the information is very small, generally add up to not more than 100 bytes, get to this write information, we can easily based on the following data to build a picture, when the data gets more and more complete, we construct the picture will be more complete, until all loading is complete.

BMP format is a simple picture format, other jpg,png Although the results are more complex, but the overall composition is similar. ImageIO is helping us to complete the codec of many image formats, and then construct the final picture step-by-step.


More iOS Madness: Http://blog.csdn.net/wanglongblog


iOS Crazy detailed ImageIO complete progressive loading picture

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.