objective-c - 异步加载和更改图像

标签 objective-c ios asynchronous uiimage nstimer

我从网站异步加载 4 张图像,加载后我用计时器更改它们,但看起来图像数组总是第一张图像,当我更改到数组的另一个位置时,它包含相同的图像,根本不要改变...

- (void)viewDidLoad 
{
    [super viewDidLoad];

    [self getImages];
}

-(void)getImages
{
    receivedData = [[NSMutableData alloc]init];
    myImages = [[NSMutableArray alloc]init];
    myImagesAdresses = [[NSMutableArray alloc]initWithCapacity:5];

    [myImagesAdresses addObject:@"adress/Images/3.png"];
    [myImagesAdresses addObject:@"adress/Images/2.png"];
    [myImagesAdresses addObject:@"adress/Images/1.png"];
    [myImagesAdresses addObject:@"adress/Images/0.png"];

    [self loadNextImage];
}

-(void)loadNextImage
{
    if([myImagesAdresses count])
    {
        NSURL * imageURL = [NSURL URLWithString:[myImagesAdresses lastObject]];
        NSURLRequest * myRequest = [NSURLRequest requestWithURL:imageURL];
        [[NSURLConnection alloc]initWithRequest:myRequest delegate:self];
        NSLog(@"Start load URL %@",imageURL);
    }
    else {
        [self.theImage setImage:[myImages objectAtIndex:2]];
        [self changeImage];
        NSLog(@"No More Images to Load");
    }
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [receivedData appendData:data];
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    [myImages addObject:[UIImage imageWithData:[NSData dataWithData:receivedData]]];
    [connection release];
    connection = nil;
    NSLog(@"Image from %@ loaded",[myImagesAdresses lastObject]);
    [myImagesAdresses removeLastObject];
    [self loadNextImage];
}

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    [connection release];
    connection =nil;
    NSLog(@"Image from %@ not loaded", [myImagesAdresses lastObject]);
    [myImagesAdresses removeLastObject];
    [self loadNextImage];
}

以及更改图像的代码:

-(void)changeImage
{
    SEL selectorToCall = @selector(change: );

    NSMethodSignature *methodSignature = [[self class] instanceMethodSignatureForSelector:selectorToCall];

    NSInvocation * invocation = [NSInvocation invocationWithMethodSignature:methodSignature];
    [invocation setTarget:self];
    [invocation setSelector:selectorToCall];

    NSTimer * newTimer = [NSTimer scheduledTimerWithTimeInterval:5.0 invocation:invocation repeats:YES];

    self.paintingTimer = newTimer;
}

- (void) change:(NSTimer *)paramTimer
{
    static NSInteger currentElement = 0;
    if(++currentElement == [myImages count]) currentElement = 0;
    [self.theImage setImage:[myImages objectAtIndex:currentElement]];
}

- (void) stopPainting
{
    if (self.paintingTimer != nil){
        [self.paintingTimer invalidate];
    }
}

我没有收到任何错误,但图像根本没有改变...

最佳答案

看起来 receivedData 是一个重复追加的 ivar,但它永远不会被清除。所以在收到第一张图片后,第二张图片被附加到它上面,依此类推。

在这一行之后,你应该清空receivedData:

[myImages addObject:[UIImage imageWithData:[NSData dataWithData:receivedData]]];

关于objective-c - 异步加载和更改图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7588101/

相关文章:

ios - 在 ObjectMapper 中调用 super 方法的问题

ios - 如何在 viewController 转换期间释放内存

Javascript:如何确保 fetch() 在使用数据之前已完成?

objective-c - 如何使用 NSPredicate 中的 "ALL"聚合操作来过滤基于 CoreData 的集合

ios - iOS 的 Facebook SDK 共享对话框错误

ios - 屏幕外 UITableViewCells(用于尺寸计算)不符合尺寸等级?

android - Flutter - 在每次应用重启后保持变量的值

Yii框架中的PHP异步方法调用

iphone - 更改 UITableViewCell 的图像

ios - 存折应用程序未在 iOS 模拟器中更新?