Asynchronous download of images

Source: Internet
Author: User

Download Image////////

Method one (violent and effective, must be downloaded before entering the page) sync

Nsurl *url = [Nsurl urlwithstring:@ "Http://cc.cocimg.com/bbs/attachment/upload/07/128707.png"];

UIImage *IMGA = [[UIImage alloc] Initwithdata:[nsdata Datawithcontentsofurl:url]];

Img1.image=imga;

Method Two (need to wait for the download to display, but can still enter the page) async

NSString *[email protected] "HTTP://TP1.SINAIMG.CN/2122519000/50/5620445947/1";

Uiimagefromurl ([Nsurl Urlwithstring:url2], ^ (UIImage * image)

{

Img2.image=image;

NSLog (@ "----%@", image);

}, ^ (void) {

NSLog (@ "error!");

} );

The following is a//////////of reading and writing documents

UIImage *bear=[uiimage imagenamed:@ "Image.png"];

Document creates a directory search path in a list. Get the Documents folder for your application

Nsarray *patharray = Nssearchpathfordirectoriesindomains (Nsdocumentdirectory,nsuserdomainmask, YES);

NSString *paths = [Patharray lastobject];

NSString *uniquepath = [[Nshomedirectory () stringbyappendingpathcomponent:@ "Documents"]

stringbyappendingpathcomponent:@ "Image.png"];

/* Write picture */

Get a name for the file.

NSString *uniquepath=[[paths stringbyappendingpathcomponent:@ "image"] stringbyappendingpathcomponent:@ " Image.png "];

Write the picture to the documents file

Get the Image object in PNG format

[Uiimagepngrepresentation (Bear) Writetofile:uniquepath Atomically:yes];

/* Read in Picture */

Because it's a path, it's loaded into a data object.

NSData *data=[nsdata Datawithcontentsoffile:uniquepath];

Just read the picture.

Img3.image=[uiimage Imagewithdata:data];

[Super Viewdidload];

}

-(void) didreceivememorywarning

{

[Super didreceivememorywarning];

}

-(void) Dealloc {

[IMG1 release];

[Img2 release];

[IMG3 release];

[Super Dealloc];

}

#pragma mark-Method two

//

void Uiimagefromurl (Nsurl * URL, Void (^imageblock) (UIImage * image), void (^errorblock) (void))

{

Presents an asynchronous execution block in the dispatch queue and returns immediately.

Dispatch_async (Dispatch_get_global_queue (dispatch_queue_priority_default, 0), ^ (void)

{

NSData * data = [[[NSData alloc] initwithcontentsofurl:url] autorelease];

UIImage * image = [[[UIImage alloc] initwithdata:data] autorelease];

Returns the serial dispatch queue associated with the main thread of the application.

Dispatch_async (Dispatch_get_main_queue (), ^ (void) {

if (image = nil)

{

Imageblock (image);

} else {

Errorblock ();

}

});

});

}

@end

Asynchronous download of images

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.