gpt4 book ai didi

ios - 委托(delegate)在操作期间释放

转载 作者:行者123 更新时间:2023-11-29 03:04:46 27 4
gpt4 key购买 nike

我正在寻找解决以下问题的解决方案:

我有一个在后台下载图像的 NSOperation:

@protocol CoreImageDownloadingOperationDelegate <NSObject>

@required
-(void) handleResponse:(UIImage*) response;
-(void) handleException:(MobileServiceException*) exception;

@end

@interface CoreImageDownloadingOperation : NSOperation{
}

-(id) initWithDelegate:(id<CoreImageDownloadingOperationDelegate>)del andImageID: (NSString *) image;

@property (nonatomic, assign) id <CoreImageDownloadingOperationDelegate> delegate;

当下载完成后,调用委托(delegate)方法,将图片设置到imageView:

pragma mark - overridden from NSOperation
- (void) main {

if (self.isCancelled)
return;

@autoreleasepool {

@try {

UIImage* image = [[CoreEnvironment sharedInstance] getImageFromServer: [imageID stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

if (self.isCancelled)
return;

if(delegate){
[delegate handleResponse:image];
}
else
NSLog(@"CachedImageView already deallocated");

}@catch (NSException *exception) {

TestLog(@"%@", exception.reason);

if (self.isCancelled)
return;

if(delegate && [exception isKindOfClass:[MobileServiceException class]])
[delegate handleException:(MobileServiceException*)exception];

}
}
}

问题是:当我在下载图像时转到另一个页面时,cachedImageView被释放,但是当imageDownloadingOperation完成下载时,委托(delegate)不为零,并且它正在尝试处理Response...当然我得到了消息发送到已解除分配...

我在 CachedImageView 中像这样分配初始化操作:

CoreImageDownloadingOperation* imageDownloadingOperation = [[CoreImageDownloadingOperation alloc] initWithDelegate:self andImageID:imageKey];

enter image description here或:

enter image description here

-[CachedImageView isKindOfClass:]: message sent to deallocated instance 0x18868550

最佳答案

The problem is: when I go to another page while the image is downloading, the cachedImageView is deallocated

通常的处理方法是在 CachedImageView 的 dealloc 中将自己作为委托(delegate)删除。喜欢

// in CachedImageView
- (void)dealloc {
// CachedImageView keeps a reference to the operation
// called imageDownloadingOperation
imageDownloadingOperation.delegate = nil;
[super dealloc];
}

关于ios - 委托(delegate)在操作期间释放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22908532/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com