zambono
zambono

Reputation: 1397

images not being added to NSMutableDictionary, which is within an NSOperation

So I first coded all my methods in a viewcontroller with an NSOperationQueue. After doing some research and a lot of reading I realized i had to subclass my loadImage operation so that I may use isCancelled and cancelAllOperations. So I went ahead and created an nsoperation class and called it from my viewcontroller. ALl the methods are called, even the imageLoaded, but the NSMutableDictionary remains empty. I use the dictionary to populate my tableviewcells using the url as the Key. Also be aware that the operation call in the viewcontroller is within a method which is called by an NSInvocationOperation when the view loads.

@interface loadImages : NSOperation {

    NSURL *targetURL;
}

@property(retain) NSURL *targetURL;

- (id)initWithURL:(NSURL*)url;

@end

implementation of nsoperation class which includes some other calls to resize the image

@implementation loadImages

@synthesize targetURL;

- (id)initWithURL:(NSURL*)url
{
    if (![super init]) return nil;
    [self setTargetURL:url];
    return self;
}

- (void)dealloc {
    [targetURL release], targetURL = nil;
    [super dealloc];
}

- (void)main {

    NSLog(@"loadImages.m reached");

    StoriesTableViewController *stories = [[StoriesTableViewController alloc] init];

    NSMutableDictionary *tempDict = stories.filteredImagesDict;

    UIImage *myImage = [[[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[self targetURL]]]autorelease]; 

    UIImage *scaledImage = [[[UIImage alloc] init] autorelease];


    CGRect frame = CGRectMake(100.0f, 100.0f, 180.0f, 180.0f);
    UIImageView *myImageFrame = [[UIImageView alloc] initWithFrame:frame];

    myImage = [[myImage croppedImage:[myImageFrame bounds]]retain];


    scaledImage = [[myImage resizedImage:CGSizeMake(120.0f, 120.0f) interpolationQuality:kCGInterpolationHigh]retain];

    [tempDict setValue:scaledImage forKey:[self targetURL]];


    [stories performSelectorOnMainThread:@selector(imageLoaded:)
                              withObject:myImage
                                        waitUntilDone:YES];

    NSLog(@"targetURL %@",[self targetURL]);
    NSLog(@"tempDict count: %d",tempDict.count);

    [stories release];
    [myImage release];
    [myImageFrame release];
    [scaledImage release];
}

creation of nsoperation on viewcontroller

for(int i=0;i<storyQuantity;i++) {
        NSString *imageString = [[[storiesArray objectAtIndex:i] objectForKey: @"image"] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];  // must add trimming to remove characters

        NSURL *url = [NSURL URLWithString:imageString];
        loadImages *imageOperation = [[loadImages alloc] initWithURL:url];
        [queue_ addOperation:imageOperation];
        [imageOperation release];

    }

Upvotes: 0

Views: 472

Answers (1)

David Beck
David Beck

Reputation: 10159

If this is running without exceptions and the dictionary is still empty, it most likely means that your value is nil.

This is a common problem with code like that where you have the result of one method going into the next. At any point if there is a problem, all the rest of the chain will not work.

To solve, I would start right above where you assigned the image to the dictionary. You can use either a breakpoint, or an NSLog to determine the value of the image at that point. I prefer to use an NSLog, but a break point would let you look at all the values at once. If scaledImage is nil, then check myImage. Keep going back up the chain until you find the point where the value goes from what you would expect, to nil.

Upvotes: 1

Related Questions