tags:

views:

50

answers:

2

where to dealloc/ release my NS-mutable array a1 ??

see this

- (void)viewDidLoad {

[NSThread detachNewThreadSelector:@selector(loadImage) toTarget:self withObject:nil];


}

- (void) loadImage
{

    NSLog(@" THREAD METHOD");





    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];  


    NSUserDefaults *imgg = [NSUserDefaults standardUserDefaults];
    myimg= [imgg stringForKey:@"keyToimg"];
    NSLog(@"RES image sssssssss  is = %@",myimg);

    a1 = [[NSMutableArray alloc] init];
    [a1 addObjectsFromArray:[myimg componentsSeparatedByString:@"\n\t"]];



    //[a1 removeAllObjects];
    //// 
    //[myimg release];
    [pool release];


}

and in table cell of secition 3 i am displaying image

switch(indexPath.section)
{

                                    NSString *urlE=[a1 objectAtIndex:1];
            NSLog(@"url is %@",urlE);

            NSData *backgroundData = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlE]]; 


            image = [UIImage imageWithData:backgroundData];
            myImageView= [[UIImageView alloc] initWithImage:image];
            [myImageView setUserInteractionEnabled:YES]; 

            CGRect rect=CGRectMake(20 ,10, 270, 180);

            myImageView.frame = rect;
            myImageView.tag = i;    
                    [cell.contentView addSubview:myImageView];

}

and based on tap images are changing

pragma mark working image tap

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    NSLog(@" life count %d",[myimg retainCount]);

    NSLog(@" life  array count %d",[a1 retainCount]);     

    //NSLog(@" GITSHffffffffffffffffffffffffffffffC");
    NSUInteger sections = [indexPath section]; 
    //NSLog(@"row is %d",sections);
    if (sections  == 3)
    { //Its either 1 or 0 I don't remember, it's been a while since I did some tableview  
        if(tap<[a1 count]-1) {
            NSLog(@" life  array count %d",[a1 retainCount]);     
            tap++;

            NSString *sa=[a1 objectAtIndex:tap];
        //////////////////////
            image= [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat: sa,[a1 objectAtIndex:tap ]]]]];


            NSData *imageData = [NSData dataWithData:UIImageJPEGRepresentation(image, 1.0)]; 


            myImageView.image = image;
            //[myimg release];
            //[a1 release];

        }
        else {
            tap=1;
            //[myimg release];
            //[a1 release];
        }



        }



        //[a1 release];


}   

so where should i release my a1 and myimg

A: 

a1 will never be released using this code. You should put it on a member variable or add autorelease after init.

By the way, your myImageView should be released after you add it to the cell view. It is possible because of the retain/release logic: when you alloc the myImageView the retain count is +1, once you add it to cell view,it is now +2, you should then release it so that the retain comes back to +1 and then when cell view will be further deallocated, it will decrement the retain count to 0.

The same logic for the variable image in the last function

Regards Meir assayag

Meir Assayag
if i auto release a1 then app just quits that why i cant use that any other idea :(
ram
A: 

Instead of :

a1 = [[NSMutableArray alloc] init];
[a1 addObjectsFromArray:[myimg componentsSeparatedByString:@"\n\t"]];

Consider:

a1 = [NSMutableArray arrayWithArray:[myimg componentsSeparatedByString:@"\n\t"]];

That'll initialize your a1 with an autoreleased NSMutableArray object, and then you don't have to worry about manually releasing it.

The thing I don't know is whether your [pool release] will release it, but... I'd really prefer you NOT put that business in a background thread, but rather use asynchronous network methods to get your image data.

By the way, as I was learning iPhone development, I went through three or four levels of "aha moments" about backgrounded networking. One of them had to do with running selectors on background threads. That lasted about a week until I discovered ASIHttpRequest, which is how I do it now. MUCH simpler way to put network interactions in the background without having to mess with threading or any of that nonsense. See http://allseeing-i.com/ASIHTTPRequest/

If you look at my answers, every time HTTP client networking comes up I recommend ASI. I really don't mean to be a shill for it--it's just made my life so much easier I think everyone needs to know about it.

Dan Ray
but how to download images on BG ... see right now i am loading new xml each time user click on any row and then i am displaying all the info of detail section in that i have to load 10 images also ..so when my image laoding is done then only i can move to detail section so right now its quite lame :(
ram
Load up your UIImageViews with placeholder images, then use the queuing functions in ASIHttpRequest to fire a series of asynchronous requests. As those requests return data, make UIImages from that data and set the .image field of each UIImageView with the new image. You've heard the term "lazy loading"? That's what that is. It results in a much faster-FEELING interface, even though the work is pushed off for later.
Dan Ray