NSThread, UITableViewCell and Image - iphone

I am having some problem with my UITableView. I am downloading the data and setting up the cells like usual, but this time with the NSThread added.
First I will call ASIHTTPRequest in my view did load
Then in - (void)requestFinished:(ASIHTTPRequest *)request I will add the url of the thumbnail into an NSMutableArray
NSString *photoURLString = [NSString stringWithFormat:#"http://some.url.com/img/%#",[thearray objectForKey:#"tn_url"]];
[thumbNailURL addObject:(photoURLString)];
And in the cellForRowAtIndexPath: where I am setting up other data to display, I will call my NSThread
[NSThread detachNewThreadSelector:#selector(loadImage:) toTarget:self withObject:indexPath];
And in my loadImage thread
- (void) loadImage: (NSIndexPath*) indexPath{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString *iconURL = [thumbNailURL objectAtIndex:indexPath.row];
NSURL *url = [NSURL URLWithString:iconURL];
UIImage *icon = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
cellImageView = [[UIImageView alloc] initWithFrame:CGRectMake(11.0, 6.0, 61.0, 50.0)];
[cellImageView setImage:icon];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
[cell.contentView performSelectorOnMainThread:#selector(addSubview:) withObject:cellImageView waitUntilDone:NO];
[pool drain];
}
All the data and image loaded just fine, but if I scroll faster up and down , the application will hang with the following error
[1997:7a1f] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 6 beyond bounds [0 .. 5]' (0x31312d2f 0x345f50e3 0x3127d1b1 0x35aefc1d 0xf4dd 0x333ffb51 0x3347c67b 0x366af589 0x366b2310) terminate called throwing an exceptionProgram received signal: “SIGABRT”.
It would be great if anyone could show me the correct configuration if I am wrong, as I have been trying different approaches for the past 3 days already.

A couple things.
First, I would recommend you consider using something like SDWebImage for async image downloading (looks like Olivier Poitrey is the original author). Github repository here: https://github.com/rs/SDWebImage. I use it for this very purpose and it's great.
That said, I see a few things here.
1) You don't seem to be checking if the cell image is already set... no need to re-download the image if you've already got it. You might get away with this since dataWithContentsOfUrl might offer some caching, but I'm not sure.
2) I can't tell from the code here, but you need to be sure you don't allow the table view to load it's data until after requestFinished is called. Otherwise you'll have a race condition that could cause the type of error you are seeing. Consider setting the tableView's data source in the requestFinished method and then calling reloadData. Otherwise, do as downed suggested and just prefill the URL array with [NSNull null] objects.

Have you tried having the array "thumbNailURL" contain [NSNull null] objects for the number of items that you are trying to display, then replacing them after you load them async, that way you can't be faster than the array.
thumbNailURL = [[NSMutableArray alloc] init];
for (unsigned i = 0; i < [items count]; i++) {
[thumbNailURL addObject:[NSNull null]];
}
Then simply:
[thumbNailURL replaceObjectAtIndex:index withObject:thumbnail];

Related

UIImage isKindOfClass issue

This is my code in my application,
[imageview setAlpha:1.0f];
[imageview setImage:[UIImage imageNamed:[NSString stringWithFormat:#"%#.png",[pages objectAtIndex:swipeCount]]]];
[imageview setFrame:CGRectMake(-300, 0, 1368, 1000)];
Edit:
pages = [[NSArray alloc] initWithObjects:#"page1",#"page2",#"page3",#"page4",#"page5",#"page6",#"page7b",#"page8",#"page9",#"page10a",#"page11",#"page12",#"page13b",#"page14",#"page15",#"page16a",#"page17",#"page18",#"page19",#"page20",#"page21",#"page22",#"page23",#"page24",#"page25", nil];
imageview=[[UIImageView alloc]init];
its working properly, problems except when the app enters background and comes back to foreground shows the following error,
*** -[UIImage isKindOfClass:]: message sent to deallocated instance 0x1e8b10
What wrong with the code?
Please help me out
Yes, you need to allocate memory to your UIImage. What is basically happening is that your image is being temporarily stored in the memory and deallocated upon closing the app so iOS could allocate that memory to more immediate needs. You can fix that as below. I'm also gonna allocate a string since stringWithFormat returns an autorelease String.
NSString *imageName = [[NSString alloc] initWithFormat::#"%#.png",[pages objectAtIndex:swipeCount]];
UIImage *myImage = [UIImage imageNamed:myImage];
[imageview setImage:image];
Try using properties. Make array,imageView as a property and then use
self.pages and self.imageView
see this good article on properties
Objective-c properties

downloading image is slow even after using Async call in iphone

I have to 20-25 download images of 50 Kb- 2 Mb each and show them in a tableview.
I used ASIHTTPRequest asyn request to this. I observed that after some time the app gets stuck. This should not happen because I am using a async call. I thought something is wrong with ASIHTTPRequest and I observed that The didFinished selector gets called in the main thread. The only thing which I do is
-(void)didFinishedDownloadingImage:(ASIHTTPRequest*)request
{
NSData *responseData = [request responseData];
UIImage *image = [UIImage imageWithData:responseData];
[[data objectAtIndex:request.tag] setImage:image];
[self.tableView reloadData];
}
I don't think this should cause any problem. Also in cellforrowatindexpath I just do
- (UItableViewCell *)tableviewView:(UItableView *)tableview
cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UserProfile * user = [data objecAtIndex:indexpath.row];
UITableViewCell *cell = [tableView
dequeueReusableCellWithReuseIdentifier:#"ProfileCell"
forIndexPath:indexPath];
if(cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewDefaultStyle];
}
NSString *fullname = [NSString stringWithFormat:#"%#\n%#",
user.firstname, user.lastname];
if(user.image != nil)
[cell.imageView setImage:user.image];
else{
[cell.imageView setImage:[UIImage imageNamed:#"placeholder.jpg"]];
}
[cell.label setText:fullname];
return cell;
}
But the app is slow and freezes for 1-2 sec which is a considerable amount of time.
I have seen apps which does this very smoothly. I tried using an image of fixed size 5Kb which has a very significance performance improvement with using the above code. I don't know why should that make a difference for big images in this case because all downloading is happening in other thread via ASIHTTP .
Please, replace your framework with AFNetworking.
You can simple use..
IImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, 100.0f)];
[imageView setImageWithURL:[NSURL URLWithString:#"http://i.imgur.com/r4uwx.jpg"] placeholderImage:[UIImage imageNamed:#"placeholder-avatar"]];
or... directly in TableViewCell
NSURL *url = [[NSURL alloc] initWithString:[movie objectForKey:#"artworkUrl100"]];
[cell.imageView setImageWithURL:url placeholderImage:[UIImage imageNamed:#"placeholder"]];
"In the second line, we tell the image view where the thumbnail is located by passing an NSURL and we pass in a placeholder image, which is shown as long as our request has not returned a response"
Thats all!
Here you have an tutorial about that http://mobile.tutsplus.com/tutorials/iphone/ios-sdk_afnetworking/
It's easy to make assumptions about the root cause of a laggy/slow application. Instead of guessing, why don't you test your suspicions? Profile your application with the Time Profiler instrument. It'll tell you which methods and functions your application is spending the most time in.
Here are some ideas until you have a chance to profile:
You might consider downloading the full-res images and creating thumbnails in the background and then caching them in an NSCache object. You can also run [UIImage imageWithData:responseData]; in a background thread. It's thread-safe until the point at which it interacts with the view hierarchy.
Selectively reloading a single cell should be faster than reloading the entire tableview, especially one with lots of images. Furthermore if you're doing all of the networking and processing on a background queue, there's no reason scrolling the tableview should be slow. Can you show us your entire implementation of the -cellForRowAtIndexPath: method? You've mentioned that you think setImage: is your slow point because rendering is slow. If you reload a single cell, only one cell needs to be rendered. If you reload the entire tableview, every cell must be re-rendered.

Where am I leaking memory here? Used Instruments

Instruments is saying there is a memory leak in this code:
- (void)layoutImageMaskViewForImageAtPath:(NSString *)path withFillColor:(UIColor *)color indexPath:(NSIndexPath *)indexPath {
UIImage *image = [UIImage imageWithContentsOfFile:path];
[self layoutImageMaskViewForImage:image withFillColor:color indexPath:indexPath];
}
UIColor *anIconFillColor = [UIColor colorWithWhite:0.70 alpha:1.0];
NSIndexPath *anIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
NSString *aPlaceholderPath = [[NSBundle mainBundle] pathForResource:#"path" ofType:#"png"];
[self layoutImageMaskViewForImage:anImage withFillColor:anIconFillColor indexPath:anIndexPath];
and
NSDictionary *anAssignedData = [aReservationData objectForKey:kAssignedSectionKey];
NSMutableArray *anEmployeeTaskQueueList = [NSMutableArray array];
NSArray *anAssignedReservationData = [anAssignedData objectForKey:kEmployeesIdentifier];
for (NSDictionary *aJobQueueData in anAssignedReservationData) {
EmployeeReservationQueue *anAssignedTaskQueue = [[EmployeeReservationQueue alloc] initWithServerDictionary:aJobQueueData];
if (anAssignedTaskQueue.rows.count == 0) {
ReservationTrack *aTrack = [[ReservationTrack alloc] init];
aTrack.rowSortOrder = 0;
aTrack.reservations = [NSArray array];
anAssignedTaskQueue.rows = [NSArray arrayWithObject:aTrack];
[aTrack release];
}
[anEmployeeTaskQueueList addObject:anAssignedTaskQueue];
[anAssignedTaskQueue release];
}
Your second example leaks track. Your last line is releasing aTrack instead.
In second case here:
[aTrack release];
What is aTrack? May be you mean [track release];?
In first case probably that you pass to function non-autoreleased parameters or may be you are not releasing them after calling that method. Just post code where you call for that method and I will check.
Gold memory-management rule in Objective-C :
Each 'init', 'copy','mutableCopy','retain' must call then 'release' or 'autorelease'.
Instruments reports that your app is leaking a ReservationTrack object. By default it shows where the leaked object was allocated, which is the code you posted. The code you posted doesn't leak a ReservationTrack. It stores it in an EmployeeReservationQueue which is stored in an NSMutableArray. One possibility is that you later access the ReservationTrack object, send it retain, and don't send it release or autorelease. Another possibility is that you leak the EmployeeReservationQueue or the NSMutableArray.
If you use the simulator, you can see the full retain/release history of most objects. When a leaked object shows up, mouse over the address of the object and click the right arrow that appears next to the address. Instruments will show you every malloc, retain, release, and autorelease event for that object. If you choose View > Extended Detail from the menu bar, you can click on any of those events and see the stack trace of the event. This should help you track down the unbalanced retain.

Program received signal: "EXC_BAD_ACCESS" while adding subview to main view

i am adding two labels and two image view to subview.
when ever i tap on the button i add this subview to mainview.
I am getting images from the web server and save it in local simulator documents.
NSMutableString *about_name_str = [[NSMutableString alloc]init];
[about_name_str appendString:[myDictionary objectForKey:#"firstname"]];
[about_name_str appendString:#" "];
[about_name_str appendString:[myDictionary objectForKey:#"lastname"]];
[about_name_label setText:about_name_str];
NSMutableString *about_addr_str = [[NSMutableString alloc]init];
[about_addr_str appendString:[myDictionary objectForKey:#"state"]];
[about_addr_str appendString:#","];
[about_addr_str appendString:[myDictionary objectForKey:#"country"]];
[about_addr_label setText:about_addr_str];
about_image.image = [UIImage imageWithContentsOfFile:imagepath];
about_logo.image = [UIImage imageWithContentsOfFile:logopath];
if ([myDictionary objectForKey:#"companyurl"]) {
[about_url_button setTitle:[myDictionary objectForKey:#"companyurl"] forState:UIControlStateNormal];
about_url_button.userInteractionEnabled = YES;
}
else {
about_url_button.userInteractionEnabled = NO;
}
[self.view addSubview:about_view];
this my code.
some times i got Program received signal: "EXC_BAD_ACCESS". and application quits.
i check by placing break points,and in debugger i did n't get where i am getting error.
can any one please help me,How can i resolve this.
Thank u in advance.
Try to use NSZombie.. It is a easy way to find where the EXCBADACCESS occurs...
It will specify which Method where and Which object gets deallocated(Its pretty awesome concept i like in Instruments)...
See this Link
http://www.markj.net/iphone-memory-debug-nszombie/
You should insert a breakpoint before this code is executed then step through it to find the exact line which is causing the bad access. You probably have a null or wild pointer somewhere.

iphone UIImage memory leaks

I'm implementing an image browser, using a UIScrollView. Due to memory costranints, I've to implement image dynamic loading (I don't want use CATiled Layers because it forces user remaining waiting to load every tile).
I've tried in a coupe of ways:
- (UIImageView*) ldPageView{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; // Top-level pool
NSError *error;
NSData *imData = [NSData dataWithContentsOfURL:ldPageRef options:NSDataReadingUncached error:&error];
UIImage *im = [[UIImage alloc] initWithData:imData];
ldView = [[UIImageView alloc] initWithImage:im] ;
[ldView setFrame:pageRect];
[pool release]; // Release the objects in the pool.
return ldView;
}
And even in this way
- (UIImageView*) ldPageView{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; // Top-level pool
CGDataProviderRef provider = CGDataProviderCreateWithURL ((CFURLRef)ldPageRef);
CGImageRef d = CGImageCreateWithJPEGDataProvider(provider,nil, true,kCGRenderingIntentDefault);
UIImage *im = [[UIImage alloc] initWithCGImage:d];
ldView = [[[UIImageView alloc] initWithImage:im] autorelease];
[im release];
CGDataProviderRelease(provider);
CGImageRelease(d);
[ldView setFrame:pageRect];
[pool release]; // Release the objects in the pool.
return ldView;
}
But every time I try it both on simulator and on iPad, memory explodes. I've runned my code with Instruments and no leak is reported. ldView is an istance variable and it is deallocated togheter with ldPageRef on object dealloc (which is called for sure).
I've also tried setting NSURLCache sharedCache to nil or to zero, but it is still happening.
I've read Memory management guide, but everythimg seems ok to me.
Please help
Try using
UIImage *im = [UIImage imageWithData:imData];
rather than
UIImage *im = [[UIImage alloc] initWithData:imData];
Always avoid allocs if possible otherwise you must ensure that you manually release the object.
More than likely it is how you are creating your UIImage. Try creating your image as such..
[UIImage imageWithData:imData];
instead of
[[UIImage alloc] initWithData:imData];
This will return a autoreleased object(it is a class method) so that you will not have to try to release it yourself later.
You are never releasing your alloc'd objects. You need to change:
[[UIImage alloc] initWithData:imData];
[[[UIImageView alloc] initWithImage:im];
to:
[[[UIImage alloc] initWithData:imData] autorelease];
[[[UIImageView alloc] initWithImage:im] autorelease] ;
Indeed I have found a memory leak in UIImageView. You just never pay any attention to it, since you may open images from the App package all the time, and these images are being cached by iOS.
But if you download a number of images from the network (say 40 iPhone-camera photos), save them to the documents and open them over and over again in a sub view controller, the memory leak applies. You do not need to have 40 different images, it is enough to load one image again and again.
Your test app is with ARC disabled and an image is being loaded from file and displayed in a sub view controller, every time that controller is being pushed.
In your sub view controller you'll create an UIImageView and assign the UIImage object to the image view's .image property. When you leave the sub view controller, you release the image view correctly. On an iPhone 4s your app won't open more than 80 images. Mine crashed at around 70 images (with iOS 6.1). And have a look on the intruments app, before the crash. The memory is full of CFMalloc blocks.
The solution I found is simple: Set the image property to nil, before you release the image view. Again, look at the instruments app. It now looks like what you'd expect and your app does no longer crash.
I think the same leak applies to whatever the UIWebView uses to display images and Apple doesn't know about it.