似乎无法用 NSArray 调用 NSInvocationOperation



我正试图从url在后台加载图像。如果我传递的只是NSUrl,那么代码就非常有效。如果我试图传递一个带有额外变量的NSArray,它永远不会被调用:

这段代码运行得很好,调用了LoadImage2,它又很好地调用了ImageLoaded2。

- (void)LoadBackgroundImage2: (char*)pImageURL
{
NSString* pImageURLString = [NSString stringWithFormat:@"%s", pImageURL];
NSLog( @"LoadBackgroundImage2: %@", pImageURLString );
NSOperationQueue *queue = [NSOperationQueue new];
NSInvocationOperation *operation = [[NSInvocationOperation alloc]
initWithTarget:self
selector:@selector(LoadImage2:)
object:pImageURLString];
[queue addOperation:operation];
[operation release];
}

- (void)LoadImage2: (NSString*)pImageURL
{
NSLog( @"LoadImage2: %@", pImageURL );
NSData* imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:pImageURL]];
UIImage* image = [[[UIImage alloc] initWithData:imageData] autorelease];
[imageData release];
[self performSelectorOnMainThread:@selector(ImageLoaded2:) withObject:image waitUntilDone:NO];
}

此代码不起作用。LoadImage从未被调用:

- (void)LoadBackgroundImage: (char*)pImageURL :(int)textureID :(int)textureType
{
printf( "LoadBackgroundImage( %s, %d, %d)n", pImageURL, textureID, textureType );
NSString* pImageURLString = [NSString stringWithFormat:@"%s", pImageURL];
NSArray* pUrlAndReferences = [[[NSArray alloc] initWithObjects: pImageURLString, textureID, textureType, nil] autorelease];
NSOperationQueue *queue = [[NSOperationQueue new] autorelease];
NSInvocationOperation *operation = [[NSInvocationOperation alloc]
initWithTarget:self
selector:@selector(LoadImage:)
object:pUrlAndReferences];
[queue addOperation:operation];
[operation release];
}

- (void)LoadImage: (NSArray*)pUrlAndReferences
{
NSString* pImageUrl = [pUrlAndReferences objectAtIndex: 0];
int textureId = [ [ pUrlAndReferences objectAtIndex: 1 ] intValue ];
int textureType = [ [ pUrlAndReferences objectAtIndex: 2 ] intValue ];
NSLog( @"nnLoadImage: %@, %d, %dn", pImageUrl, textureId, textureType );
NSData* pImageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:pImageUrl]];
UIImage* pImage = [[[UIImage alloc] initWithData:pImageData] autorelease];
NSArray* pImageAndReferences = [[[NSArray alloc] initWithObjects: pImage, textureId, textureType, nil] autorelease];
[pImageData release];
[self performSelectorOnMainThread:@selector(ImageLoaded:) withObject:pImageAndReferences waitUntilDone:NO];
}

有人知道为什么LoadImage没有被调用吗?

谢谢。

我的猜测是您没有保留您的队列。以下是正在发生的事情

  1. 您的数组(自动释放)进入NSInvocationOperation并被保留(没有问题)
  2. 您的NSInvocationOperation进入队列(保留),然后它被释放。这里没有问题,因为保留计数仍然是1:1(alloc)+1(retain)-1(release)=1=未解除锁定
  3. 您的队列被分配(new=alloc+init),然后自动释放,但它不会保留在其他地方。问题来了:由于您已经自动释放了队列,一旦方法LoadBackgroundImage完成,队列的保留计数为0,并且它会自动释放,因此,您的Invocation将不会执行

如果这是问题所在,您可以尝试从队列中删除autorelease调用。如果我是正确的,你的代码应该可以工作。但要注意,这不是一个好的解决方案,因为你正在失去记忆。只是看看它是否有效。

您肯定应该创建一个类、一个singleton、一个实例变量或任何您喜欢的东西来保留队列的实例。此外,对于所有LoadBackgroundImage调用,最好只有一个队列,而不是每次都创建一个新队列。

最新更新