Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development > iPhone SDK Development - Advanced Discussion

Reply
 
LinkBack Thread Tools Display Modes
Old 10-08-2010, 05:12 AM   #1 (permalink)
Tukuche
 
Join Date: Jun 2010
Posts: 6
ajitthakali is on a distinguished road
Default Image Download

Is there better way to download Image from a Server to phone.I use NSURL but it crashes sometimes.
Here is the code i used:

UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.12312312.com/image.png"]]];
NSString *docDir3 = [NSSearchPathForDirectoriesInDomains(NSDocumentDire ctory, NSUserDomainMask, YES) objectAtIndex:0];

NSString *pngFilePath3 = [NSString stringWithFormat:@"%@/Landing Image.png",docDir3];
NSData *data3 = [NSData dataWithData:UIImagePNGRepresentation(image)];
[data3 writeToFilengFilePath3 atomically:YES];
ajitthakali is offline   Reply With Quote
Old 10-08-2010, 11:41 AM   #2 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Aug 2008
Location: Memphis, TN, USA
Age: 24
Posts: 3,983
smithdale87 is on a distinguished road
Send a message via AIM to smithdale87
Default

what is the filesize of the image you are trying to download?
smithdale87 is offline   Reply With Quote
Old 10-08-2010, 04:33 PM   #3 (permalink)
Super Moderator
 
Join Date: Oct 2009
Location: San Diego, CA
Posts: 1,586
JasonR is on a distinguished road
Default

Loading large files through synchronous methods is generally a bad idea. The whole app will crash if it takes too long. Here's the link I based my image loading class around: UITables with Downloaded Images – Easy Asynchronous Code | markjnet
JasonR is offline   Reply With Quote
Old 10-10-2010, 10:08 PM   #4 (permalink)
Registered Member
 
jimmy's Avatar
 
Join Date: Aug 2010
Posts: 52
jimmy is on a distinguished road
Default

Quote:
Originally Posted by smithdale87 View Post
what is the filesize of the image you are trying to download?
I have around 24 large images with sizes between 100-140 kb...and thumbs images are 4kb....Is there any problem?thanks for the response..
jimmy is offline   Reply With Quote
Old 10-19-2010, 01:34 PM   #5 (permalink)
Nuisance Developer
 
Join Date: Jul 2009
Location: Italy
Posts: 4,691
dany_dev is on a distinguished road
Default

yes, of course, run with leaks and look the allocations.
__________________
dany_dev is offline   Reply With Quote
Old 10-22-2010, 07:47 PM   #6 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,002
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by ajitthakali View Post
Is there better way to download Image from a Server to phone.I use NSURL but it crashes sometimes.
Here is the code i used:

UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.12312312.com/image.png"]]];
NSString *docDir3 = [NSSearchPathForDirectoriesInDomains(NSDocumentDire ctory, NSUserDomainMask, YES) objectAtIndex:0];

NSString *pngFilePath3 = [NSString stringWithFormat:@"%@/Landing Image.png",docDir3];
NSData *data3 = [NSData dataWithData:UIImagePNGRepresentation(image)];
[data3 writeToFilengFilePath3 atomically:YES];
There are several things wrong with this code.

First and most importantly, It reads data synchronously. That will freeze your UI, which is a bad user experience. The system may even terminate your app if you remain unresponsive for too long. Another poster to this thread posted a link that showed how to do async downloads. (Describing Async downloading is beyond the scope of a single post.)

Secondly, your code reads the data from the server, turns it into a UIImage, then converts the image to NSData, copies that data into ANOTHER NSData object, and then saves that data to a file. Thus you create 3 copies of each image in memory.

You should skip all those intermediate steps and just write the data from the server directly to a file.


Code:
NSData* fileData = [NSData dataWithContentsOfURL:[NSURL   
   URLWithString:@"http://www.12312312.com/image.png"]];
NSString *docDir3 = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *pngFilePath3 = [NSString stringWithFormat:@"%@/Landing Image.png",docDir3];
[fileData writeToFile:pngFilePath3 atomically:YES];[/quote]
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Reply

Bookmarks

Tags
download, image, server

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 380
15 members and 365 guests
ChrisYates, coolman, davejas69, Free App Monster, givensur, iAppDeveloper, jbro, Kryckter, locombiano89, Mah6447, Meoz, stanny, stevenkik, Tomsky, WeaselPig
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,646
Threads: 94,111
Posts: 402,862
Top Poster: BrianSlick (7,990)
Welcome to our newest member, locombiano89
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 05:48 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0