1. NSData datawithcontentsofurl// [Self.icon setimage:[uiimage imagewithdata:[nsdata DataWithContentsOfURL: Tempurl]]; 2. Dispath form added to asynchronous processing// [self imagedownloadbyurlasync:tempurl complete:^ (UIImage *image) {// [Self.icon setimage : image];// }]; 3. Currently I choose the way to download side loading mode with cgimageref imagewithcgimage _request = [[Nsurlrequest alloc] initwithurl:tempurl]; _conn = [[Nsurlconnection alloc] initwithrequest:_request delegate:self]; _incrementallyimgsource = Cgimagesourcecreateincremental (NULL); _recievedata = [[Nsmutabledata alloc] init]; _isloadfinished = false; Self.icon.alpha =. 5; Self.lblTitle.alpha =. 5; [Self.lbltitle Settext:appname];
The first way, is basically very few people use the most basic way this way there is a problem is the network bad case will card main thread, causing the program suspended animation
The second way, the implementation code of the paragraph, please
-(void) Imagedownloadbyurlasync: (nsurl *) URL complete: (All) finished//{// //asynchronous parallel execution// Dispatch_ Async (Dispatch_get_global_queue (dispatch_queue_priority_default, 0), ^{// UIImage *image = nil;// Nserror * error;// NSData *responsedata = [NSData datawithcontentsofurl:url options:nsdatareadingmappedifsafe error:& error];// image = [UIImage imagewithdata:responsedata];// //Jump back to the home row execution// Dispatch_async (dispatch_get_ Main_queue (), ^{// //UI action in the main queue// finished (image);// }) ;//}
Although the situation is the same as the first implementation, the execution code is added to the corresponding asynchronous execution and then successfully downloaded after the image is taken to the main thread to execute the callback setting.
The third way requires the following code this is my Baidu to the way
#pragma mark-nsurlconnectiondatadelegate-(void) connection: (nsurlconnection*) connection didreceiveresponse: ( nsurlresponse*) response{_expectedleght=response.expectedcontentlength; NSLog (@ "Expectedlength:%lld", _expectedleght); Nsstring*mimetype=response. MIMEType; NSLog (@ "mimetype%@", MIMETYPE); Nsarray*arr=[mimetype componentsseparatedbystring:@ "/"]; if (arr.count<1| |! [[arr objectatindex:0] isequal:@ "image"]) {NSLog (@ "Notaimageurl"); [Connection Cancel]; _conn=nil; }}-(void) connection: (nsurlconnection*) connection didfailwitherror: (nserror*) error{NSLog (@ "connection% @error, errorinfo:%@ ", connection,error);} -(void) connectiondidfinishloading: (nsurlconnection*) connection{NSLog (@ "connectionloadingfinished!!!"); Ifdownloadimagedatanotcomplete,createfinalimage if (!_isloadfinished) {Cgimagesourceupdatedata (_incrementallyIm Gsource, (Cfdataref) _recievedata,_isloadfinished); Cgimageref Imageref=cgimagesourcecreateiMageatindex (_incrementallyimgsource,0,null); UIImage * Image=[uiimage Imagewithcgimage:imageref]; [Self.icon Setimage:image]; 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); UIImage * Image=[uiimage Imagewithcgimage:imageref]; [Self.icon Setimage:image]; Cgimagerelease (IMAGEREF);}
This method has been tested by me very useful but I do not know if there will be any bugs just used and the user experience will increase accordingly
IOS Load Network Pictures 2