Stumped on UIImage/NSThread

Discussion in 'iOS Development' started by NolesFans, May 27, 2009.

  1. NolesFans

    NolesFans New Member

    Joined:
    Sep 23, 2008
    Messages:
    404
    Likes Received:
    0
    Device:
    iPod touch
    in
    Code:
    - (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
    I have
    Code:
    RSSData *newsForNow = [stories objectAtIndex:indexPath.row];
            if ([newsForNow image]) {
                [NSThread detachNewThreadSelector:@selector(grabDataFromImageURL:) toTarget:self withObject:[newsForNow image]];
                cell.imageView.image = remoteImage;
            }
    
    Code:
    - (void)grabDataFromImageURL:(NSString*)urlString {
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
        NSURL *url = [NSURL URLWithString:urlString];
        NSData *data = [NSData dataWithContentsOfURL:url];
        [self performSelectorOnMainThread:@selector(resizeImageFromData:) withObject:data waitUntilDone:YES];
        [pool release];
    }
    
    - (void)resizeImageFromData:(NSData*)imageData {
        UIGraphicsBeginImageContext( CGSizeMake(53, 53) );
        UIImage *tmpImage = [[UIImage alloc] initWithData:imageData];
        [tmpImage drawInRect:CGRectMake(0, 0, 53, 53)];
        self.remoteImage = UIGraphicsGetImageFromCurrentImageContext();
        [tmpImage release];
        UIGraphicsEndImageContext();
    }
    It didn't show any image.

    Please Register or Log in to view images

  2. Cyanidepoison

    Cyanidepoison Active Member

    Joined:
    Jan 18, 2008
    Messages:
    1,076
    Likes Received:
    2
    EDIT2:

    UIKit isn't thread-safe.
  3. NolesFans

    NolesFans New Member

    Joined:
    Sep 23, 2008
    Messages:
    404
    Likes Received:
    0
    Device:
    iPod touch
    I can't send any data from NSData in a new thread into Main Thread?
  4. Cyanidepoison

    Cyanidepoison Active Member

    Joined:
    Jan 18, 2008
    Messages:
    1,076
    Likes Received:
    2
    I think I'm misunderstanding your code again, sorry. I just feel like you're doing this the wrong way. Let me find one of my projects that does what you want...

    You could just try setting the image's frame instead of all the graphics context stuff.
  5. gojohnnyboi

    gojohnnyboi Well-Known Member

    Joined:
    Jan 25, 2008
    Messages:
    3,339
    Likes Received:
    55
    What I think you're doing wrong is that you need to send a message back to the main thread notifying it that you are indeed done grabbing / resizing the image, or whatever you please.
  6. NolesFans

    NolesFans New Member

    Joined:
    Sep 23, 2008
    Messages:
    404
    Likes Received:
    0
    Device:
    iPod touch
    That is for UIImageView to set frame.

    @johnny, what kind of message of that?
  7. gojohnnyboi

    gojohnnyboi Well-Known Member

    Joined:
    Jan 25, 2008
    Messages:
    3,339
    Likes Received:
    55
    Look at NSObject's header.

    Code:
    - (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait
    create a done method, and call it.

    Also, may I suggest NSOperation/NSInvocationOperation to you for threading, they work wonders.
  8. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    You're doing it wrong. Don't spawn a new thread in the cellForRow. Spawn it when the table is no longer moving.

    Also, all UIKit methods NEED to be done in the main thread. Also, what's with all the autoreleased objects expected to work after you drain the autorelease pool? And, while we're on the topic of autorelease, you should get in the habit of using [pool drain] instead of [pool release]. Please read the docs for an understanding of why.
  9. Cyanidepoison

    Cyanidepoison Active Member

    Joined:
    Jan 18, 2008
    Messages:
    1,076
    Likes Received:
    2
    Okay then.
  10. NolesFans

    NolesFans New Member

    Joined:
    Sep 23, 2008
    Messages:
    404
    Likes Received:
    0
    Device:
    iPod touch
    I thought I have UIKit method in the main thread like UIImage.

Share This Page