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