我使用下面的代码下载了图像,但每当我尝试显示它们时,什么都没有出现。我已经测试了从应用程序(在模拟器中)将图像下载到我的桌面,并且所有图像都创建得很好。所以我知道它们被保存到应用程序的 Documents 目录中。
关于我做错了什么的任何想法?
下载图像代码:
-(void)saveThumbnails:(NSMutableArray *)thumbnails{
for(NSNumber *videoID in thumbnails){
// Get an image from the URL below
NSURL *thumbnailURL = [self generateThumbnailURL:[videoID intValue]];
UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:thumbnailURL]];
NSLog(@"%f,%f",image.size.width,image.size.height);
// Let's save the file into Document folder.
// You can also change this to your desktop for testing. (e.g. /Users/kiichi/Desktop/)
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);//Find Application's Document Directory
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"DownloadedThumbnails"];
// NSString *dataPath = @"/Users/macminidemo/Desktop/gt";//DEBUG SAVING IMAGE BY SAVING TO DESKTOP FOLDER
//Check if Sub-directory exists, if not, try to create it
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath]){
NSError* error;
if([[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error]){
NSLog(@"New Folder Created!");
}
else
{
NSLog(@"[%@] ERROR: attempting to write create new directory", [self class]);
NSAssert( FALSE, @"Failed to create directory maybe out of disk space?");
}
}
NSArray *splitFilename = [[self generateThumbnailFilename:[videoID intValue]] componentsSeparatedByString:@"."];//Break Filename Extension Off (not always PNGs)
NSString *subString = [splitFilename objectAtIndex:0];
NSString *formattedFilename = [NSString stringWithFormat:@"%@~ipad.png",subString];
NSString *localFilePath = [dataPath stringByAppendingPathComponent:formattedFilename];
NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(image)];
[imageData writeToFile:localFilePath atomically:YES];
NSLog(@"Image: %@ Saved!",formattedFilename);
}
}
显示图像代码:
if(currentVideo.thumbnail!=(id)[NSNull null] || ![currentVideo.thumbnail isEqualToString:@"missing_image.png"]){
NSArray *splitFilename = [currentVideo.thumbnail componentsSeparatedByString:@"."];//Break Filename Extension Off (not always PNGs)
NSString *subString = [splitFilename objectAtIndex:0];
NSString *formattedFilename = [NSString stringWithFormat:@"%@~ipad.png",subString];
UIImage *thumbnailImage = [UIImage imageNamed:formattedFilename];
NSLog(@"Image Name: %@",formattedFilename);
[videoThumbnail setImage:thumbnailImage];
[buttonVideo addSubview:videoThumbnail];
}else{
UIImage *thumbnailImage = [UIImage imageNamed:@"video-details-thumbnail"];
[videoThumbnail setImage:thumbnailImage];
[buttonVideo addSubview:videoThumbnail];
}
*并且显示 buttonVideo 或类似的东西没有问题。我已经测试了只是设置了一个测试图像,并且一个显示很好(即单独运行时的“else”语句使用默认缩略图就可以正常工作)