iOS - 无法显示下载的图像

时间:2012-12-11 16:56:18

标签: ios uiimage nsdata

我使用以下代码下载图像,但每当我尝试显示它们时,都不会显示任何内容。我已经测试了从应用程序(在模拟器中)将图像下载到我的桌​​面,所有图像都创建得很好。所以我知道它们被保存到App的文档目录中。

关于我做错的任何想法?

下载图片代码:

-(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”语句使用默认缩略图工作得很好)

1 个答案:

答案 0 :(得分:2)

imageNamed的文档说明它适用于应用主要捆绑中的文件。这告诉我,它仅适用于作为应用内资源提供的图像。尝试imageWithContentsOfFile:获取文档目录的内容。