异步加载和更改图像

发布于 2024-12-07 14:29:49 字数 2738 浏览 4 评论 0原文

我从网站异步加载 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];
    }
}

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

Im loading 4 images from a website asynchronous,and after the loading i change them with a timer,but looks like on the array of the images is always the first image,when i change to another position of the array it contains the same image,dont change at all...

- (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];
}

and the code to change the image:

-(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];
    }
}

I dont getting any error,but the image simply dont change...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

英雄似剑 2024-12-14 14:29:49

看起来 receivedData 是一个您重复追加的 ivar,但它永远不会被清除。因此,在收到第一个图像后,将附加第二个图像,依此类推。

在此行之后,您应该清空 receivedData

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

It looks like receivedData is an ivar that you repeatedly append to, but it never gets cleared. So after the first image is received, the second image is appended to it, etc.

After this line, you should empty receivedData:

[myImages addObject:[UIImage imageWithData:[NSData dataWithData:receivedData]]];
顾北清歌寒 2024-12-14 14:29:49

您应该在:之后添加这行代码,

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

如下所示:

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

You should add this line of code after:

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

like this:

[myImages addObject:[UIImage imageWithData:[NSData dataWithData:receivedData]]];
[receivedData setData:nil];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文