2

我正在尝试从 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( @"\n\nLoadImage: %@, %d, %d\n", 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 没有被调用?

谢谢。

4

1 回答 1

1

我的猜测是你没有保留你的队列。这是正在发生的事情

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

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

您绝对应该创建一个类、一个单例、一个实例变量或任何您喜欢保留该队列实例的东西。此外,最好只为所有LoadBackgroundImage呼叫设置一个队列,而不是每次都创建一个新队列。

于 2012-11-01T12:05:35.660 回答