외부 소스에서 GIF를로드하고 화면에 표시하는 Mac OS X 용 Object C 및 Cocoa의 응용 프로그램을 작성하고 있습니다. 사용자는 용어를 검색하고 GIF를 다운로드하여 NSImageViews에 배치 한 다음 해당보기를 스크롤보기에 표시합니다.NSThread를 사용할 때 GIF 애니메이션이 적용되지 않습니다.
이전에 GIF는 예상대로 애니메이션되었습니다. 그런 다음 NSThread를 사용하여 GIF로드를 수행하고 주 스레드와 별도로 스크롤보기에 추가하려고했습니다.
- (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];
스레드를 만들려면 전화 : 여기에 스레드에서 호출되는 방법이다. 이미지는 화면에 표시되지만 첫 번째 프레임 만 표시됩니다. 애니메이션이 적용되지 않습니다. 글자 그대로 스레드 초기화를 스레드 내부의 코드로 대체하면 GIF가 애니메이션 처리된다는 점을 제외하면 동일한보기가 렌더링됩니다. GIF에 애니메이션을 적용하지 못하게하는 스레드가 있습니까?
감사합니다.
UIKit과 마찬가지로 AppKit은 근본적으로 스레드 안전하지 않습니다. 이 기능이 전혀 작동하지 않는다는 사실은 기적이며 아마도 버그입니다. – CodaFi
아, 이해했다. 그렇다면 후속 질문 - 현재 창을 고정시키고 죽음의 해변 공을 표시하지 않고 외부 이미지를로드하는 방법에 대해 살펴 보시겠습니까? – user73171
주 스레드에서 가져온 이미지 배열을 반환하는 완료 블록을 사용하는 변형 된 메서드를 작성합니다. – CodaFi