I found on some forum post about saving image from web site to iphone app and I’m using it because everything is ok with it except one thing, speed.
Image is about 10KB and it’s downloaded with ok speed when there is one image, but when there are 15-20 images it’s very slow.
I know there must be way to download image faster on another way becase I have some news apps on my iPhone and this apps download 15-20 articles with images like my (I say like my because quality is the same or better) and is about 5 seconds faster (or more).
So my question is; Is there another faster way to download images from website to iPhone app?
This is my code:
#define kBgQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) //1
#import "downloadImage.h"
@interface downloadImage ()
@end
@implementation downloadImage
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *imgUrl = @"http://somesite.com/someimage.jpg";
dispatch_async(kBgQueue, ^{
[self performSelectorOnMainThread:@selector(downloadImageFromWeb:) withObject:imgUrl waitUntilDone:YES];
});
}
-(void)downloadImageFromWeb:(NSString *)imgUrl{
UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imgUrl]]];
NSArray *parts = [imgUrl componentsSeparatedByString:@"/"];
NSString *imgFilename = [parts lastObject];
[self saveImage:image:imgFilename];
}
- (void)saveImage:(UIImage*)image:(NSString*)imageName {
NSData *imageData = UIImageJPEGRepresentation(image, 100);
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:
imageName];
[fileManager createFileAtPath:fullPath contents:imageData attributes:nil];
//NSLog(@"image saved");
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
1) check the actual load time for this image from a normal browser, this might be a server-side issue
2) try to use some proven code for this, like AsyncImageView and see if there is any difference with yours.
3) remove saving and test again. If it goes faster, subthread the saving (however, it is not good idea to create so many threads)
4) why do you perform load selector on main thread ? Do it on background thread and later update the image on the main one. The way you are dispatching it now it blocks the main thread.