Tableview Images Loading Slowly - objective-c

I am trying to load image from a JSON feed into a tableview. The code below returns the images but it is making the app very very slow and "sticky". Any ideas on how to make it faster? This is being called for each cell in the tableview.
NSURL *imageURL = [NSURL URLWithString:#"http://www.site.com/images/image_1.jpg"];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage *cellImage = [UIImage imageWithData:imageData];
Thank you all!

Please try the below code.
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, queue, ^{
NSURL *imageURL = [NSURL URLWithString:#"http://www.site.com/images/image_1.jpg"];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage *cellImage = [UIImage imageWithData:imageData];
dispatch_sync(group, ^{
cell.image = cellImage;
});
});
dispatch_release(group);

Loading of images from the Internet in main thread is always bad idea.
You need to subclass UITableViewCell and do loading of images in background thread.
Here you have few suggestions how to do it.
Lazy load images in UITableView
Load image to a tableView from URL iphone sdk

Related

Adjusting UIImageView size and location based on the size of image fetched from service

In my iOS7 app I have view just like profile page in any social networking site. I have a profile image in top left corner. I am fetching the image from service url and loading it to a UIImageview.
My problem is, I have to increase the size of this image view when the size of the image from service is big. And also i have to adjust other UILabels beside this image view. I am using this code to get the size of image from url:
NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data cache:NO];
CGSize size = img.size;
I don't know initWithData:cache: method on UIImage class, is this category thing?
Anyway you can make UIImageView dimension to fit content UIImage by [imageView sizeToFit].
If you want to adjust scale, use [UIImage imageWithData:data scale:scale].
NSString *path = #"http://www.example.com/example#x2.png"
NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data scale:2.0];
_imageView.image = img;
[_imageView sizeToFit];

saving images from a url takes a long time

The below code successfully get the image from the link and stores into my cache directory. But I want to get many(like 100) images from different url(but on the same website, only the filename differs). This works fine for taking those images but i need to wait for a long time. is there anyway to get the images easily and make my responsive time really faster.
NSString *UCIDLink = [NSString stringWithFormat:#"http://www.example.com/picture.png];
NSURL * imageURL = [NSURL URLWithString:UCIDLink];
NSData * imageData = [NSData dataWithContentsOfURL:imageURL];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:#"picture.png"]];
NSError *writeError = nil;
[imageData writeToFile:filePath options:NSDataWritingAtomic error:&writeError];
if (writeError) {
NSLog(#"Success");
}else{
NSLog(#"Failed");
}
ghgh
The code you are using take time to load image contents. so, prefer to load image asynchronously.
use below code:
dispatch_queue_t q = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(q, ^{
/* Fetch the image from the server... */
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
dispatch_async(dispatch_get_main_queue(), ^{
/* This is the main thread again, where we set the tableView's image to
be what we just fetched. */
cell.imgview.image = img;
});
});
or you can use:
AsyncImageView *asyncImageView = [[AsyncImageView alloc]initWithFrame:CGRectMake(30,32,100, 100)];
[asyncImageView loadImageFromURL:[NSURL URLWithString:your url]];
[YourImageView addSubview:asyncImageView];
[asyncImageView release];
Download the Files from here.....
https://github.com/nicklockwood/AsyncImageView
Use multi-threading in order to make a number of image fetches happen simultaneously. That way, you can reduce your waiting time a lot.

Upload pictures to my app

I´m designing a new app for an iPad. This app will work with a barcode scanner, and when it scans a code, it will show an image asociated.
I was thinking to build that asociating de barcode to an image name for example:
- Barcode 09090909 will show 09090909.png picture
- Barcode 19191919 will show 19191919.png picture
....
I think that there is no problem with that, but the problem comes when I need to add new barcode/pictures to the app. How can I send a new picture to my App? I see that when you develop on XCode and build you App, all the data goes into de App.
Any help or clue? thanks in advance
You can simple load them on the fly from your server, but then a network connection is required. To load them async from an server you can use this code:
dispatch_queue_t image_queue = dispatch_queue_create("com.company.app.imageQueue", NULL);
dispatch_queue_t main_queue = dispatch_get_main_queue();
dispatch_async(image_queue, ^{
NSString *URLString = #"http://example.com/images/19191919.png";
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:URLString];
UIImage *image = [UIImage imageWithData:imageData];
dispatch_async(main_queue, ^{
[imageView setImage:image];
});
});
If you want you can also save the image to the cache dir (using the document dir is not recommended because it will collide with Apples iOS Data Storage Guidelines) and check before downloading an image twice, then the code will look like this:
NSString *fileName = #"19191919.png";
NSString *URLBaseString = #"http://example.com/images/";
NSString *URLString = [URLBaseString stringByAppendingString:fileName];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachePath = [paths objectAtIndex:0];
NSString *dataPath = [cachePath stringByAppendingPathComponent:fileName];
NSFileManager *fileManager = [NSFileManager defaultManager];
if ( [fileManager fileExistsAtPath:dataPath] )
{
UIImage *image = [UIImage imageWithContentsOfFile:dataPath];
[imageView setImage:image];
}
else
{
dispatch_async(image_queue, ^{
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:URLString];
UIImage *image = [UIImage imageWithData:imageData];
[UIImagePNGRepresentation(image) writeToFile:dataPath atomically:YES];
dispatch_async(main_queue, ^{
[imageView setImage:image];
});
});
}
This code has not been tested, but should work. Note that I'm relying on ARC to manage memory.

Improve speed - UIImage from NSURL

I'm using the following code to fetch some images, that are going into a tableview. But it takes ages (5-6 seconds) to get the 30 images in.
Is there a smarter/faster way to do this?
NSString *imageUrl = ......;
NSString *urlStr =
[imageUrl UrlstringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *path = [[NSString alloc] initWithFormat:#"http://xxx.dk/xml.aspx%#", urlStr];
NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
return img;
[..... release];
Here profileimg is an get from Api ,String name
NSString *profileimg=[Check objectForKey:#"IMAGE"];
imageview.image=[[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:profileimg]]];
I'd rather download every new image in array of images when a cell appears on the screen and just display already saved image if a cell appears on the screen for the second time (i.e. image for this cell has been already downloaded).
use asynchronize loading of image.
check out this sample code : http://developer.apple.com/library/ios/#samplecode/LazyTableImages/Introduction/Intro.html

Getting Image from URL Objective C

I'm trying to get an image from an URL and it doesn't seem to be working for me. Can someone point me in the right direction?
Here is my code:
NSURL *url = [NSURL URLWithString:#"http://myurl/mypic.jpg"];
NSString *newIMAGE = [[NSString alloc] initWithContentsOfURL:url
encoding:NSUTF8StringEncoding error:nil];
cell.image = [UIImage imageNamed:newIMAGE];
When I debug the newIMAGE string is nil so something isn't working there.
What you want is to get the image data, then initialize a UIImage using that data:
NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: #"http://myurl/mypic.jpg"]];
cell.image = [UIImage imageWithData: imageData];
[imageData release];
As requested, here's an asynchronous version:
dispatch_async(dispatch_get_global_queue(0,0), ^{
NSData * data = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: #"http://myurl/mypic.jpg"]];
if ( data == nil )
return;
dispatch_async(dispatch_get_main_queue(), ^{
// WARNING: is the cell still using the same data by this point??
cell.image = [UIImage imageWithData: data];
});
[data release];
});
Ok there's a couple of things wrong here:
The conversion from URL (url) to NSString (newImage) is incorrect, what the code actually does there is try to load the contents of "http://myurl/mypic.jpg" into the NSString.
The -imageNamed method takes a string that is a path of a local file, not a URL as an argument.
You need to use an NSData object as an intermediary, like in this example:
http://blogs.oreilly.com/digitalmedia/2008/02/creating-an-uiimage-from-a-url.html
the accepted answer asynchronous version worked very slow in my code. an approach using NSOperation worked light years faster. the code provided by Joe Masilotti --> objective - C : Loading image from URL? (and pasted below):
-(void) someMethod {
// set placeholder image
UIImage* memberPhoto = [UIImage imageNamed:#"place_holder_image.png"];
// retrieve image for cell in using NSOperation
NSURL *url = [NSURL URLWithString:group.photo_link[indexPath.row]];
[self loadImage:url];
}
- (void)loadImage:(NSURL *)imageURL
{
NSOperationQueue *queue = [NSOperationQueue new];
NSInvocationOperation *operation = [[NSInvocationOperation alloc]
initWithTarget:self
selector:#selector(requestRemoteImage:)
object:imageURL];
[queue addOperation:operation];
}
- (void)requestRemoteImage:(NSURL *)imageURL
{
NSData *imageData = [[NSData alloc] initWithContentsOfURL:imageURL];
UIImage *image = [[UIImage alloc] initWithData:imageData];
[self performSelectorOnMainThread:#selector(placeImageInUI:) withObject:image waitUntilDone:YES];
}
- (void)placeImageInUI:(UIImage *)image
{
[self.memberPhotoImage setImage:image];
}
In Swift 3 and 4
let theURL = URL(string:"https://exampleURL.com")
let imagedData = NSData(contentsOf: theURL!)!
let theImage = UIImage(data: imagedData as Data)
cell.theImageView.image = theImage
This will be done in the main thread.
And to perform the same in asynchronous/background thread
DispatchQueue.main.async(){
let theURL = URL(string:"https://exampleURL.com")
let imagedData = NSData(contentsOf: theURL!)!
let theImage = UIImage(data: imagedData as Data)
}
cell.theImageView.image = theImage
Updating upon Jim dovey answer,[data release] is no longer required because in the updated apple guidelines. Memory management is done automatically by ARC (Automatic counting reference) ,
Here is the updated asynchronous call,
dispatch_async(dispatch_get_global_queue(0,0), ^{
NSData * data = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: #"your_URL"]];
if ( data == nil )
return;
dispatch_async(dispatch_get_main_queue(), ^{
self.your_UIimage.image = [UIImage imageWithData: data];
});
});