异步 NSImage 下载:第一个图像无限加载
Asynchronous NSImage download: first image has infinite loading
我正在尝试使用 NYTPhotoViewer 和异步图像下载制作一个用于本机反应的模块。它可以工作,但是当其他图片下载和显示时,第一张图片永远不会停止加载动画。有趣的是,如果我滑动一些图像(2 个或更多),第一个图像将呈现。代码在这里:
#import <NYTPhotoViewer/NYTPhotosViewController.h>
#import "NYTImageViewer.h"
#import "NYTExamplePhoto.h"
@interface NYTImageViewer () <NYTPhotosViewControllerDelegate>
@property (nonatomic) NSArray *photos;
@end
@implementation NYTImageViewer
RCT_EXPORT_MODULE()
RCT_EXPORT_METHOD(open:(NSArray *)urls)
{
dispatch_async(dispatch_get_main_queue(), ^{
NSArray *photos = addPhotos(urls);
NYTPhotosViewController *photosViewController = [[NYTPhotosViewController alloc] initWithPhotos:photos];
UIViewController *ctrl = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
[ctrl presentViewController:photosViewController animated:TRUE completion: nil];
});
}
NSArray* addPhotos(NSArray *urls) {
NSMutableArray *photos = [NSMutableArray array];
for (int i = 0; i < [urls count]; i++) {
NYTExamplePhoto *photo = [[NYTExamplePhoto alloc] init];
dispatch_async(dispatch_get_global_queue(0,0), ^{
NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: urls[i]]];
if ( imageData == nil )
return;
dispatch_async(dispatch_get_main_queue(), ^{
photo.image = [UIImage imageWithData: imageData];
});
});
[photos addObject:photo];
}
return photos;
}
@end
我该如何解决?
你不应该t/can从远程 URL 以这种方式加载,并且 dispatch_async
它会产生预期的行为:
NSData * _data = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: urls[i]]];
您必须使用 NSURLSession
或 NSURLConnection
通过异步方法进行远程调用,并且您必须在回调中处理递归。
我正在尝试使用 NYTPhotoViewer 和异步图像下载制作一个用于本机反应的模块。它可以工作,但是当其他图片下载和显示时,第一张图片永远不会停止加载动画。有趣的是,如果我滑动一些图像(2 个或更多),第一个图像将呈现。代码在这里:
#import <NYTPhotoViewer/NYTPhotosViewController.h>
#import "NYTImageViewer.h"
#import "NYTExamplePhoto.h"
@interface NYTImageViewer () <NYTPhotosViewControllerDelegate>
@property (nonatomic) NSArray *photos;
@end
@implementation NYTImageViewer
RCT_EXPORT_MODULE()
RCT_EXPORT_METHOD(open:(NSArray *)urls)
{
dispatch_async(dispatch_get_main_queue(), ^{
NSArray *photos = addPhotos(urls);
NYTPhotosViewController *photosViewController = [[NYTPhotosViewController alloc] initWithPhotos:photos];
UIViewController *ctrl = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
[ctrl presentViewController:photosViewController animated:TRUE completion: nil];
});
}
NSArray* addPhotos(NSArray *urls) {
NSMutableArray *photos = [NSMutableArray array];
for (int i = 0; i < [urls count]; i++) {
NYTExamplePhoto *photo = [[NYTExamplePhoto alloc] init];
dispatch_async(dispatch_get_global_queue(0,0), ^{
NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: urls[i]]];
if ( imageData == nil )
return;
dispatch_async(dispatch_get_main_queue(), ^{
photo.image = [UIImage imageWithData: imageData];
});
});
[photos addObject:photo];
}
return photos;
}
@end
我该如何解决?
你不应该t/can从远程 URL 以这种方式加载,并且 dispatch_async
它会产生预期的行为:
NSData * _data = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: urls[i]]];
您必须使用 NSURLSession
或 NSURLConnection
通过异步方法进行远程调用,并且您必须在回调中处理递归。