• Home
  • Basics
  • DSA
  • MAD
  • Concept
  • Practice
  • Misc
    • Tips
    • QA’s
    • Misc
  • Course
  • Home
  • Basics
  • DSA
  • MAD
  • Concept
  • Practice
  • Misc
    • Tips
    • QA’s
    • Misc
  • Course
  • #News
  • #APPS
  • #Apple WWDC
  • #Google I/O
  • #Microsoft Ignite
  • #Let’s Talk
  • #Advertise

MyCodeTips mycodetips-newlogocopy1

  • Home
  • Basics
  • DSA
  • MAD
  • Concept
  • Practice
  • Misc
    • Tips
    • QA’s
    • Misc
  • Course
IOS

Tips to Loading image from a URL to UIImage object

Tips to Loading image from a URL to UIImage object

Load image pointed by URL, for example, http://mycodetips.com/images/image0.jpg, using NSURL, NSURLRequest, and NSURLConnection

NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:

[NSURLRequest requestWithURL:

[NSURL URLWithString:@“http://mycodetips.com/images/image0.jpg”]] delegate:self];

Implement delegate methods of NSURLConnection

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error

- (void)connectionDidFinishLoading:(NSURLConnection *)connection

NSMutableData object can be used store data fragment whenever it is received

self.imageData = [NSMutableData data];

…

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

{

[self.imagedata appendData:data];

}

Sample code

- (void)startDownload

{

self.imageData = [NSMutableData data];

// alloc+init and start an NSURLConnection; release on completion/failure

NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:

[NSURLRequest requestWithURL:

[NSURL URLWithString:imageURLString]] delegate:self];

self.imageConnection = conn;

[conn release];

}

- (void)cancelDownload

{

[self.imageConnection cancel];

self.imageConnection = nil;

self.imageData = nil;

}

#pragma mark -

#pragma mark Download support (NSURLConnectionDelegate)

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

{

[self.activeDownload appendData:data];

}- (

void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error

{

// Clear the activeDownload property to allow later attempts

self.imageData = nil;

// Release the connection now that it’s finished

self.imageConnection = nil;

}- (

void)connectionDidFinishLoading:(NSURLConnection *)connection

{

// Set appIcon and clear temporary data/image

UIImage *image = [[UIImage alloc] initWithData:self.imageData];

// Now we have loaded image in ‘image’

// DO WHATEVER WE WAN WITH ‘image’

// call our delegate and tell it that our icon is ready for display

[delegate appImageDidLoad: image];

[image autorelease];

// clean-up

self.imageData = nil;

// Release the connection now that it’s finished

self.imageConnection = nil;

}
  • Click to share on Reddit (Opens in new window)
  • Click to share on Facebook (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Tumblr (Opens in new window)
  • More
  • Click to share on Pocket (Opens in new window)
  • Click to share on Pinterest (Opens in new window)
Written by Ranjan - March 10, 2014 - 1703 Views
Tags | IOS, UIImage
AUTHOR
Ranjan

This website is basically about of what we learnt from my years of experience as a software engineer on software development specifically on mobile application development, design patterns/architectures and its changing scenarios, security, troubleshooting, tools, tips&tricks and many more.

You Might Also Like

mycodetips Xcode

Tips for Bounds and Frames in IOS

December 23, 2013
mycodetips-swift

Evolutions of Swift Programming Language

November 1, 2020
and-or-not-condition

Operators in Swift (AND, OR, or NOT.)

August 6, 2021
Next Post
Previous Post

Support us

Subscribe for updates

Join 8,278 other subscribers

Latest Posts

  • Exploring Single Point Failure
    Exploring Single Point Failures: Causes and Impacts
  • primitive-datatypes-new
    Exploring the Pros and Cons of Primitive Data Types
  • best practice clean code
    Essential Coding Standards and Best Practices for Clean Code
  • YT-Featured-Templates--lld
    What Business Problems Solves in Low Level Design (LLD)
  • SRP-SingleResopnsibility
    SRP : Single Responsibility Principle in Swift and Objective-C
whiteboard

Whiteboard(PRO)

whiteboard

Whiteboard(lite)

alphabets

Kids Alphabet

do2day

Do2Day

  • #about
  • #myapps
  • #contact
  • #privacy
  • #Advertise
  • #myQuestions

Android Database Interview IOS IOSQuestions Javascript Objective-c Programming Swift Tips&Tricks Web Wordpress

  • Exploring Single Point Failures: Causes and Impacts
  • Exploring the Pros and Cons of Primitive Data Types
  • Essential Coding Standards and Best Practices for Clean Code
MyCodeTips

©mycodetips.com

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.