Thursday, December 20, 2012

Downloading images asynchronously in background thread.

Don't you think,downloading the images in a direct way will block you main thread.Yes ofcourse ,so i come up with the simple class library called ImageDownloader,you just need to pass the url and imageview frame and its all done.

Lets have a quick look at it,first get the library from  here. And include ImageDownloader class into your project.

Few lines of coding goes here.

 NSURL *url=[NSURL URLWithString:@"ur image url"];
    ImageDownloader *imageView = [[ImageDownloader alloc] initWithFrame:CGRectMake(10,    5,120, 100)];
    //imageView.contentMode = UIViewContentModeScaleAspectFill;
    imageView.clipsToBounds = YES;
    imageView.tag = IMAGE_VIEW_TAG;
   [[ImageLoader sharedLoader] cancelLoadingURL:imageView.imageURL];
    //load the image
    imageView.imageURL = url;
     [urView addSubview:imageView];

The same concept i used in the "Talking to JSON"  sample code :)

No comments:

Post a Comment