nsthread - NSAutoreleasePool issues on the iPhone -
I have a small iPhone app that has a button on the first view when I select this button. I load new view in which it has an image. I currently allow the user to continue loading the image from an online source on an individual thread, using the following code, while controlling the application while: < / P>
- (zero) loadImageTest {NSAutoreleasePool * pool = [[NSAUTOri Yarepul Alok] Init]; NSDRAEL * URL = [[NSUR AL Alok] Init]; Url = [NSURL URLWithString: logoPath]; NSDTA * data = [[NSDTTA alloc] INIT]; Data = [nsData datatystem content of URL: url]; LoadingImage = [UIImage imageWithData: data]; Title LogoImage.image = loadingImage; // [pool drain]; [Bridge release]; }
This is called the init of the new view from this line of code:
[NSThread detachNewThreadSelector: @selector (loadImageTest) toTarget: self withObject : Zero];
Now it works fine (ISH), but if I close the new view and then again in a quick succession (or in some cases after bus) once again If loaded, then with the classic EXC_BAD_ACCESS the bomb will be sure that this thread is due to the code within the pool, but can it see why this is happening?
Thanks
Your:
// You can try NSURLConnections asynchronous methods instead of creating your own thread. [NSThread detachNewThreadSelector: @selector (loadImageTest) toTarget: auto withObject: zero];
- (zero) loadImageTest {// this is the right NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; // You are creating and then discarding this URL object will leak NSDL * url = [[NSROML Alok] INIT]; // This is exactly URL = [ns URL URLWithString: logop]; // Then creating an object and making NSDT and discarding * data = [[NSDTA Elok] init]; Data = [nsData datatystem content of URL: url]; // This works, but the thread is not secure, // // can not work on the UIKit object from the main thread loading ioges = [UIImage imageWithData: data]; Title LogoImage.image = loadingImage; // it's ok / [pool drain]; [Bridge release]; }
Things have been cleaned to make things safer, etc. Need help:
// I'm assuming that you have an iVar for logopad but we do not want to call a shorthand iVar (this is not unstable, you can do it , But this is just a bad form) // If I am wrong about the logo then having a iVar is not sweating it copying - (zero) Jumaththavatvit {NSString * aLogoPath = [[Logopath copy]] Aurore; [NSThread detachNewThreadSelector: @Selector (Load ImageForPath :) to target: Object with self: ALOOPP]; } - (zero) Load image path: (NSSTING *) ALOPATH {NSAUTOriasPool * pool = [[NSAUTOERIPUL ALLOCK] IIT]; NSDTA * Data = [NSDTATA DATAVITY CONTENT OFF URL: [NSDL URL YouthString: ALOOPP]]; // PerformSelector data can be made until the method can be made by [self performSelectorOnMainThread: @selector (setImageForTitleLogo :) withObject: imgData waitUntilDone: NO]; [Bridge release]; } - (zero) setImageForTitleLogo: (NSData *) imgData {// This part is not strictly required // But it is a good way to wrap that one method needs to be on the main thread. If ([NSThread isMainThread]) {// I create a image (I think loadingImage means to be a local scope variable) UIImage * loadingImage = [UIImage imageWithData: imgData]; // Make sure that the button still exists / you are also making sure that you are setting any reference to this button to zero when you are releasing and creating new ideas / so that you A button that is not addressed is issued // (By specifying the image to the button, the button will retain it for you) if (titleLogoImage! = Nil) titleLogoImage.image = loadingImage; } And {{self display selector OnMainThread: @selector (setImageForTitleLogo :) withObject: imgData waitUntilDone: NO]; }}
Comments
Post a Comment