我正在自定义照片滚动条 xcode 源。对于平铺图像,我想在后台使用 nsoperation 从 Web 服务器下载图像。
该应用程序会正确下载瓷砖图像,但不会刷新。不确定,如何在下载完成后立即刷新平铺图像。任何提示将不胜感激。
- (UIImage *)tileForScale:(CGFloat)scale row:(int)row col:(int)col
{
// Step 1
// format the target and source folder name using store id, flyer id and page number
// format the tile name using folder name and the tile col and row
// initiate the background process to download the target file, if required
tileName = [NSString stringWithFormat:@"%@_%d_%d_%d.png", imageName, (int)(scale * 1000), col + 1, row + 1];
[self startBackground];
// Step 2
NSString *targetFileName = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"tmp/%@",tileName]];
// NSLog(@"Return- %@",targetFileName);
UIImage *image = [UIImage imageWithContentsOfFile:targetFileName];
return image;
}
- (void)startBackground
{
NSOperationQueue *queue = [NSOperationQueue new];
NSInvocationOperation *operation = [[NSInvocationOperation alloc]
initWithTarget:self
selector:@selector(downloadAsRequired:)
object:tileName];
[queue addOperation:operation];
[operation release];
}
- (void)downloadAsRequired:(NSString*)imageTileName
{
// Steps
// format target file
// check if target file exists
NSString *targetFileName = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"tmp/%@",imageTileName]];
NSFileManager *fileManager =[NSFileManager defaultManager];
NSData *dataFromFile = nil;
dataFromFile = [fileManager contentsAtPath:targetFileName];
if (dataFromFile==nil)
{
// file doesn't exist
NSString *folderName = [NSString stringWithFormat:@"S%@F1/P%d/",[flyer.storeIdentifier stringValue],index + 1];
NSString *sourceFileName = [NSString stringWithFormat:@"%@%@%@",kLocationTiles,folderName,imageTileName];
NSData* imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:sourceFileName]];
// UIImage* image = [[[UIImage alloc] initWithData:imageData] autorelease];
NSLog(@"%@-%@",sourceFileName,targetFileName);
BOOL fileSaved = [fileManager createFileAtPath:targetFileName contents:imageData attributes:nil];
if(!fileSaved)
{
NSLog(@"failed to copy tile");
}
else
{
NSLog(@"%@ created",targetFileName);
}
[imageData release];
// [self performSelectorOnMainThread:@selector(displayImage:) withObject:image waitUntilDone:NO];
}
else
{
// file exists, so do nothing
}
}