gpt4 book ai didi

objective-c - 使用 NSThread 时 GIF 不显示动画

转载 作者:行者123 更新时间:2023-12-03 17:12:43 28 4
gpt4 key购买 nike

我正在使用 Objective C 和 Cocoa for Mac OS X 编写一个应用程序,该应用程序从外部源加载 GIF 并将其显示在屏幕上。用户搜索一个术语,GIF 被下载并放入 NSImageViews 中,然后这些 View 显示在 ScrollView 中。

之前,GIF 按预期进行了动画处理。然后,我尝试使用 NSThread 来加载 GIF 并将其添加到与主线程分开的 ScrollView 中。这是在线程中调用的方法:

    - (void)threading:(id)obj
{

NSURL *url = [NSURL URLWithString: @"URL HERE"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

[request setURL:url];
[request setHTTPMethod:@"GET"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

NSError *error;
NSURLResponse *response;
NSDictionary *data = [NSJSONSerialization JSONObjectWithData:[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error] options:NSJSONReadingMutableLeaves error:nil];


NSArray *gifs = [data objectForKey:@"data"];


for (int i = 0; i < 1; i++) { // try to load 1 gif
NSString *currentGifURL = @"whatever";
NSImageView *imgView = [[NSImageView alloc] initWithFrame:CGRectMake(10, 1820-100*i, 150, 120)];
// I tried using these 2 lines, still didn't work
//[imgView setAnimates:YES]; [imgView setImageScaling:NSScaleNone];
NSURL *imageURL = [NSURL URLWithString:currentGifURL];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
NSImage *image = [[NSImage alloc] initWithData:imageData];
imgView.image = image;
[_scrollView.documentView addSubview:imgView];

}
[_progressIndicator stopAnimation:self];
}

我稍后打电话

[NSThread detachNewThreadSelector:@selector(threading:) toTarget:self withObject:nil];

创建线程。图像出现在屏幕上,但仅显示第一帧——它们不是动画的。从字面上看,用线程内部的代码替换线程初始化会呈现相同的 View ,但 GIF 是动画的。线程中是否有某些因素会阻止 GIF 动画?

谢谢!

最佳答案

NSImageView 不是线程安全的。使用 - (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait

- (void)threading:(id)obj
{

NSURL *url = [NSURL URLWithString: @"URL HERE"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

[request setURL:url];
[request setHTTPMethod:@"GET"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

NSError *error;
NSURLResponse *response;
NSDictionary *data = [NSJSONSerialization JSONObjectWithData:[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error] options:NSJSONReadingMutableLeaves error:nil];


NSArray *gifs = [data objectForKey:@"data"];


for (int i = 0; i < 1; i++) { // try to load 1 gif
NSString *currentGifURL = @"whatever";

// I tried using these 2 lines, still didn't work
//[imgView setAnimates:YES]; [imgView setImageScaling:NSScaleNone];
NSURL *imageURL = [NSURL URLWithString:currentGifURL];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
NSImage *image = [[NSImage alloc] initWithData:imageData];

[self performSelectorOnMainThread:@selector(LoadImageView:) withObject:image waitUntilDone:NO];

}
[_progressIndicator stopAnimation:self];
}
-(void)LoadImageView : (NSImage *)image
{
NSImageView *imgView = [[NSImageView alloc] initWithFrame:CGRectMake(10, 1820-100*i, 150, 120)];
imgView.image = image;
[_scrollView.documentView addSubview:imgView];
}

关于objective-c - 使用 NSThread 时 GIF 不显示动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17101577/

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