我正在用這個類加載圖像。當需要調用dealloc時,如何阻止它們加載,我需要幫助。這個類正在擴展UIImageView。還有任何其他建議設計類是讚賞,我想例如分發加載的字節。停止從一個新線程加載的照片
@implementation RCPhoto
- (id)initWithFrame:(CGRect)frame delegate:(id)d {
if ((self = [super initWithFrame:frame])) {
// Initialization code
delegate = d;
self.contentMode = UIViewContentModeScaleAspectFit;
}
return self;
}
- (void)dealloc {
[super dealloc];
}
#pragma mark Load photo
- (void)loadImage:(NSString *)path {
// Create a new thread for loading the image
[NSThread detachNewThreadSelector:@selector(load:)
toTarget:self
withObject:path];
}
- (void)load:(NSString *)path {
// You must create a autorelease pool for all secondary threads.
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
//NSLog(@"RCPhoto loadImage into a new thread: %@", path);
NSURL *url = [NSURL URLWithString: path];
NSData *data = [NSData dataWithContentsOfURL: url];
UIImage *image = [[UIImage alloc] initWithData: data];
[self performSelectorOnMainThread: @selector(performCompleteEvent:)
withObject: image
waitUntilDone: NO];
[pool release];
}
- (void)performCompleteEvent:(UIImage *)image {
//NSLog(@"RCPhoto finish loading");
[self setImage:image];
self.opaque = YES;
if ([delegate respondsToSelector:@selector(onPhotoComplete)]) {
[delegate performSelector:@selector(onPhotoComplete)];
}
}
@end
謝謝你,僅此而已。 – 2010-11-16 07:11:32