Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Yours:</p> <p>// This is ok, you might try using NSURLConnections asynchronous methods instead of making // your own thread. [NSThread detachNewThreadSelector:@selector(loadImageTest) toTarget:self withObject:nil];</p> <pre><code>- (void)loadImageTest { // This is fine NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; // you're making and then abandoning this url object so it will leak NSURL *url = [[NSURL alloc] init]; // this is fine url = [NSURL URLWithString:logoPath]; // again making and abandoning an object NSData *data = [[NSData alloc] init]; data = [NSData dataWithContentsOfURL:url]; // this works, but is not thread safe, // can't operate on UIKit objects off the // main thread loadingImage = [UIImage imageWithData:data]; titleLogoImage.image = loadingImage; // this is fine //[pool drain]; [pool release]; } </code></pre> <p>Cleaned up to make things thread safe, etc. Should help:</p> <pre><code>// I'm assuming you have a iVar for logoPath but we don't want to // make threaded calls to an iVar (it's not mutable, so you could do it, but it's just bad form) // If i'm wrong about logoPath being an iVar don't sweat copying it. - (void)whateverMethodYouWant { NSString *aLogoPath = [[logoPath copy] autorelease]; [NSThread detachNewThreadSelector:@selector(loadImageForPath:) toTarget:self withObject:aLogoPath]; } - (void)loadImageForPath:(NSString *)aLogoPath { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:aLogoPath]]; // the performSelector will retain the data until the method can be performed [self performSelectorOnMainThread:@selector(setImageForTitleLogo:) withObject:imgData waitUntilDone:NO]; [pool release]; } - (void)setImageForTitleLogo:(NSData *)imgData { // This part is not strictly necessary // but it's a nice way to wrap a method that needs to happen on the main thread. if ([NSThread isMainThread]) { // make the image (i'm assuming you meant loadingImage to be a local scope variable) UIImage *loadingImage = [UIImage imageWithData:imgData]; // make sure the button still exists // also make sure you're setting any references to this button to nil when you're releasing and making new views // so that you're not addressing a button that's been released // (assigning the image to the button will cause the button to retain it for you) if (titleLogoImage != nil) titleLogoImage.image = loadingImage; } else { [self performSelectorOnMainThread:@selector(setImageForTitleLogo:) withObject:imgData waitUntilDone:NO]; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload