ImageIO Complete incremental load picture
First, the common progressive load picture mode
The incremental load we see today is mainly based on the following three implementations:
&NBSP
1) Loading different sizes of pictures from the web in turn, from small to large. First pull a small thumbnail to do the stretching display, and then pull the medium size of the figure, pull the end of the direct coverage of the display, and finally pull the original image, pull after the completion of the original display.
&NBSP
2 pulls the largest picture directly from the Web, displaying a bit of picture for every bit of data, which will result in a little bit of refreshing from top to bottom.
&NBSP
3) combined with the 1th and 2nd, pull a thumbnail to do the stretch display, and then use the second method to pull the original image directly, so that you can achieve incremental loading, you can save several times in the middle of the network request.
Second, incrementally loading the picture through ImageIO
ImageIO Guide The central dialect says so: "If you have a very Lar GE image, or are loading image data over the Web, you can want to create a incremental image source so, The image data as you accumulate it.
Translates: "If you want to load a particularly large picture, or load a picture from a network, you can achieve incremental loading by creating a imagesource." "The translation is not very authentic, this is probably the meaning, in the past when doing Powercam, when in order to deal with the oversized image on iOS also tried this method, when the test was a map of China, the resolution is 10000*8000, the result is when the whole picture loaded into memory, Memory is too unbearable, so give up. Now think about this super large picture processing, we can use the way of fragmentation, each time only need to deal with a small picture can, this question is left to think about it.
&NBSP
Today we are going to discuss the cgimagesource implementation of incrementally loading pictures from the Web side, and to achieve this we need to create a urlconnnection and then implement the agent to update the picture each time the data is received. The following main implementation source code:
Copy Code code as follows:
//
Svincrementallyimage.m
Svincrementallyimage
//
Created by Maple on 6/27/13.
Copyright (c) 2013 Maple. All rights reserved.
//
#import "SvIncrementallyImage.h"
#import <ImageIO/ImageIO.h>
#import <CoreFoundation/CoreFoundation.h>
@interface Svincrementallyimage () {
Nsurlrequest *_request;
Nsurlconnection *_conn;
Cgimagesourceref _incrementallyimgsource;
Nsmutabledata *_recievedata;
Long Long _expectedleght;
BOOL _isloadfinished;
}
@property (nonatomic, retain) uiimage *image;
@property (nonatomic, retain) uiimage *thumbimage;
@end
@implementation Svincrementallyimage
@synthesize ImageURL = _imageurl;
@synthesize image = _image;
@synthesize thumbimage = _thumbimage;
-(ID) Initwithurl: (Nsurl *) ImageURL
{
self = [super init];
if (self) {
_imageurl = [ImageURL retain];
_request = [ Nsurlrequest alloc] Initwithurl:_imageurl];
_conn = [[Nsurlconnection alloc] Initwithrequest:_ Request Delegate:self];
_ Incrementallyimgsource = Cgimagesourcecreateincremental (NULL);
_recievedata = [ Nsmutabledata alloc] init];
_isloadfinished = false;
}
return self;
}
#pragma mark-
#pragma Mark 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 the ImageSource data each time you receive the data, and then call Cgimagesourcecreateimageatindex to get the latest picture.
&NBSP
How to see the implementation of the above is not a sense of implementation from the Web to gradually load the picture is very simple, although imageio to help us do a lot of things, but we should also understand its principles. We know that files are formatted, the head of the general file will record some data about the file format, followed by the actual file data.
Takes an example of the simplest BMP picture file:
1) The first Bitmapfileheader, which is the primary record of the file size and the distance from the actual image data to the head of the file.
2) then Bitmapinfoheader, which mainly records the picture's wide, high, bit depth information
3) Optional palette information
4 The last part is the real The picture data of the interstate. The information in the first three parts of
is small, generally add up to no more than 100 bytes, get this write information, we can easily based on the following data to build a picture, when the data acquisition is more and more complete, we constructed the picture will be more complete, until all load completed. The
BMP format is a simple picture format, and other jpg,png, although the results are more complex, the overall composition is almost the same. ImageIO is helping us to complete a number of image format codec, and then step-by-step construction of the final picture.
use ImageIO to get EXIF information for a picture
In addition to the pixel information we can see, a picture contains information such as time taken, aperture size, and exposure. The UIImage class hides all these details, providing only the size of the picture we care about, the direction of the picture, and so on. We can get all the information behind the picture through the ImageIO frame, let's take a look at the following.
The ImageIO framework is a slightly lower-level frame in iOS, and its internal interfaces are C-style, and critical data is stored using corefoundation. Fortunately, there are many data types in corefoundation that can be seamlessly bridged by data types in the upper-level Data foundation framework. This also greatly facilitates us to the picture information operation.
Cgimagesourceref is the entire ImageIO entrance, through which we can finish loading pictures from the file. After loading, we get a cgimagesourceref, through cgimagesourceref we can get the size of the picture file, UTI (uniform type identifier), the interior contains several pictures, Visit each picture and get the EXIF information corresponding to each picture.
You may have a question, why are there a few pictures?
I explain this, imagesourceref and files are one by one corresponding, usually we see a picture file (such as jpg,png) inside only one picture, this situation we get through the Cgimagesourcegetcount method will be 1. But you can't rule out a picture file with a variety of pictures, such as GIF files, a file may contain several or even dozens of pictures. I wrote a blog in front of "How to parse and display GIF in ios" is the ability to load and parse GIF through ImageSource.
Here is the EXIF information for the photo taken by the system camera:
Copy Code code as follows:
Image property: {
ColorModel = RGB;
Dpiheight = 72;
Dpiwidth = 72;
Depth = 8;
orientation = 6;
Pixelheight = 2448;
Pixelwidth = 3264;
' {Exif} ' = {
Aperturevalue = "2.526069";
Brightnessvalue = "-0.5140446";
ColorSpace = 1;
Componentsconfiguration = (
1,
2,
3,
0
);
datetimedigitized = "2013:06:24 22:11:30";
datetimeoriginal = "2013:06:24 22:11:30";
Exifversion = (
2,
2,
1
);
Exposuremode = 0;
Exposureprogram = 2;
Exposuretime = "0.06666667";
Fnumber = "2.4";
Flash = 16;
Flashpixversion = (
1,
0
);
Focallenin35mmfilm = 33;
Focallength = "4.13";
Isospeedratings = (
400
);
Meteringmode = 3;
Pixelxdimension = 3264;
Pixelydimension = 2448;
Scenecapturetype = 0;
Sensingmethod = 2;
Shutterspeedvalue = "3.906905";
Subjectarea = (
2815,
1187,
610,
612
);
whitebalance = 0;
};
"{GPS}" = {
Altitude = "27.77328";
Altituderef = 0;
Latitude = "22.5645";
Latituderef = N;
Longitude = "113.8886666666667";
Longituderef = E;
TimeStamp = "14:11:23.36";
};
' {TIFF} ' = {
DateTime = "2013:06:24 22:11:30";
make = Apple;
Model = "IPhone 5";
orientation = 6;
Resolutionunit = 2;
Software = "6.1.4";
XResolution = 72;
YResolution = 72;
"_ycbcrpositioning" = 1;
};
}
From this we can see that the first few items show the current picture's color mode, color depth, x,y direction of the DPI, the actual pixel and the direction of the picture. When I first saw this direction, the heart of a happy this is not uiimage in the imageorientation, but the experiment found that this direction and uiimage in the imageorientation is not equal, where the direction of the EXIF standard definition of the direction, From 1 to 8 correspond to the 8 directions in this uiimage, but in a different order, they correspond to the following relationships:
Copy Code code as follows:
enum {
Exiforientationup = 1,//Uiimageorientationup
Exiforientationdown = 3,//Uiimageorientationdown
Exiforientationleft = 6,//Uiimageorientationleft
Exiforientationright = 8,//Uiimageorientationright
These four exiforientation does are not support by all camera, but IOS support and these orientation
exiforientationupmirrored = 2,//uiimageorientationupmirrored
exiforientationdownmirrored = 4,//uiimageorientationdownmirrored
exiforientationleftmirrored = 5,//uiimageorientationleftmirrored
exiforientationrightmirrored = 7,//uiimageorientationrightmirrored
};
typedef Nsinteger Exiforientation;
Most digital cameras and mobile phones in the market now have a directional sensor built into them, and the photos will be written as directional information, but usually there are only the first four different directions. These mirrored directions are usually set when the front camera of the phone is photographed.
Why does EXIF have to go in such a direction?
Almost all of the cameras in the appearance of the phase of the chip are in a direction, the photos taken out of the pixels are the default direction. If you rotate each of these pixels in a single shot, the rotation will be time-consuming if the digital camera takes 20 shots per second. A smarter way to do this is to record only one Direction when you take a picture and then display it in a direction. So EXIF defines a standard directional parameter, as long as the software to read the map to comply with the rules, load time to read the direction of the picture, and then do the appropriate rotation. This can achieve the goal of rapid imaging, but also to achieve the correct display, why not?
Common image browsing and editing software follow this rule, but there is one of our most commonly used look-and-see software (Windows comes with the map program) will not read this direction, so we will be the digital camera and mobile phone pictures to import windows, we often encounter the problem of wrong direction. I don't know what the Windows Empire is thinking, maybe it's a holiday with the organization that defines EXIF.
In addition to the picture information mentioned above, as well as shooting GPS information, iOS photo album software in the Location tab is based on GPS information. There are a lot of other information, interested in the can write a program of their own research, here is not launched.