解决这个问题的最好方法是使用委托。这意味着如果您告诉委托方法制作图像,它可以在后台执行并告诉您的视图“嘿,现在我为您获取了 XXX 的图像”(取决于您如何实现它。这样您就可以加载您的查看其中的电子书,并在书的中间显示一个带有默认背景的加载器。图像完成后,您使用正确的图像更新书的视图并删除加载器。就像苹果的iBooks 和任何其他好的应用程序都可以。
我自己的一个项目的示例(根据您的需要进行了调整,但在 UITableViewController 中使用):
BookDelegate.h
#import <Foundation/Foundation.h>
@class Book;
@protocol BookDelegate <NSObject>
@optional
- (void)didRecieveImageForBook:(NSString*)imagePath indexPath:(NSIndexPath*)indexPath;
@end
书.h
#import <Foundation/Foundation.h>
#import "BookDelegate.h"
@interface Book : NSOperation <NSObject>
{
id <BookDelegate> delegate;
SEL didRecieveImageForBookSelector;
}
@property (strong, nonatomic) id delegate;
@property (assign) SEL didRecieveImageForBookSelector;
- (NSString*)getBookImageForBookId:(int)BookId externalRefference:(NSString*)url indexPath:(NSIndexPath*)indexPath;
- (id)delegate;
// Delegate methods
- (void)didRecieveImageForBook:(NSString*)imagePath indexPath:(NSIndexPath*)indexPath;
@end
书.m
#import "Book.h"
#import <objc/runtime.h>
@implementation Book
static char kAssociationKey;
@synthesize didRecieveImageForBookSelector;
@synthesize delegate;
- (id)init
{
if (self = [super init])
{
[self setDidRecieveImageForBookSelector:@selector(didRecieveImageForBook:indexPath:)];
}
return self;
}
#pragma mark -
#pragma mark The default delegate functions
- (void)didRecieveImageForBook:(NSString*)imagePath indexPath:(NSIndexPath*)indexPath
{
NSLog(@"********************************************************");
NSLog(@"*** PLEASE IMPLEMENT THE FOLLOWING DELEGATE FUNCTION ***");
NSLog(@"*** didRecieveImageForBook:indexPath: ***");
NSLog(@"********************************************************");
}
#pragma mark -
#pragma mark Function for fechting images
// This method is not adapted to what YOU need, but left my code here in case it might help you out.
- (NSString*)getBookImageForBookId:(int)bookId externalRefference:(NSString*)url indexPath:(NSIndexPath*)indexPath
{
NSString *ext = [[url lastPathComponent] pathExtension];
NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *imagePath = [documentsPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@%d.%@", APP_BookIMAGEPEFIX, BookId, ext]];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:imagePath];
if (fileExists)
{
return imagePath;
}
else {
NSURL *theUrl = [NSURL URLWithString:url];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:theUrl];
[request setDidFinishSelector:@selector(BookImageFetched:)];
[request setDidFailSelector:@selector(processFailed:)];
[request setTimeOutSeconds:60];
[request setDownloadDestinationPath:imagePath];
[request setDelegate:self];
[request startAsynchronous];
objc_setAssociatedObject(request, &kAssociationKey, indexPath, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
return @"";
}
}
- (void)BookImageFetched:(ASIHTTPRequest *)request
{
NSIndexPath *indexPath = objc_getAssociatedObject(request, &kAssociationKey);
NSString *imagePath = request.downloadDestinationPath;
[[self delegate] performSelector:self.didRecieveImageForBookSelector withObject:imagePath withObject:indexPath];
}
#pragma mark -
#pragma mark delegate functions
- (id)delegate
{
return delegate;
}
- (void)setDelegate:(id)newDelegate
{
delegate = newDelegate;
}
#pragma mark -
@end