在表视图中显示图像,该图像保存在文档目录中

时间:2013-03-26 07:39:11

标签: ios objective-c tableview

我已将图像保存在文档目录中,并附有以下代码,之后图像成功存在于文档目录中

UIImage *image =[[UIImage alloc ]init ];
 image = [UIImage imageNamed:@"PlaceHolder.png"];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString* path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithString: @"PlaceHolder.png"] ];
    NSData* data = UIImagePNGRepresentation(image);
    [data writeToFile:path atomically:YES];

现在我想在表格视图中显示图像 在我的表视图中,一些图像从facebook url显示,这就是为什么我使用以下代码

NSString *path = [[_arrayForTable valueForKey:@"URL"] objectAtIndex:[indexPath row]];

NSURL *url = [NSURL URLWithString:path];
AsyncImageView *imageView = [[[AsyncImageView alloc] init] autorelease];
imageView.frame = CGRectMake(0, -5, 45, 45);
imageView.imageURL=url;

_arrayForTable是Dic的数组,它包含一些链接来自facebook的URL,一些来自文档目录

现在我的问题如下

**只有facebook图像显示在表格视图中,但文档目录图像未显示**

我检查过的位置是完全正确的图像,但现在显示

6 个答案:

答案 0 :(得分:1)

NSURL *url = [NSURL URLWithString:path];

您的代码中存在错误。用这行代码替换它。

NSURL *url = [NSURL fileURLWithPath:path];

检查文件目录中是否存在文件

if([[NSFileManager defaultManager] fileExistsAtPath:path])
    NSURL *url = [NSURL fileURLWithPath:path];

如果图像被保存,请检查文档目录。您将获得像这样的图像名称

image = [UIImage imageNamed:@"PlaceHolder.png"];

因此,如果您重复此行,您的图像将被覆盖在文档目录中。

您也可以在此处查看 BOOL success = [data writeToFile:path atomically:YES];

if(success)
   NSLog(@"image written successfully");
else
   NSLog(@"image writing failed");

希望我帮助过。

答案 1 :(得分:1)

在你的.m文件中

@interface YourClass ()<NSURLConnectionDelegate>
@property (nonatomic, copy) NSMutableData *receivedImageData;
@end

@implementation YourClass

- (void)init {
    if (self = [super init]) {
        // Do your init stuff
        _receivedImageData = [[NSMutableData alloc] initWithCapacity:0];
    }
}

// A method to start your connection
- (void)startDownloadingImage {
    NSMutableURLRequest *downloadImageRequest = [[NSMutableURLRequest alloc] init];
    downloadImageRequest.URL = [NSURL URLWithString:@"http://www.theImage.you/want/to/download.extension"];
    downloadImageRequest.HTTPMethod = @"GET";
    [NSURLConnection connectionWithRequest:downloadImageRequest delegate:self];

}

// You need the following delegate methods
#pragma mark -
#pragma mark NSURLConnection delegate methods

- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse {
    return request;
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {

    // This method is called frequently ... you can see if you log
    // NSLog(@"I am downloading");
    [self.receivedImageData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    // Log an error or do whatever you want ;)
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    // Create your picture here using
    UIImage *image = [UIImage imageWithData:self.receivedImageData scale:1.0];
    // Only if there is an image display the downloaded image
    if (CGSizeEqualToSize(self.imageView.image.size, CGSizeZero)) {
        self.imageView.image = image;
    }

}

您可以尝试下载图片,如果没有图片,您可以继续显示PlaceHolder.png 确保您 复制 图片,它确实在您的项目目录中

// self.imageView is an UIImageView
self.imageView.image = [UIImage imageNamed:@"PlaceHolder.png"];
[self startDownloadingImage];

让我知道它是否对你有帮助;)

答案 2 :(得分:0)

您是否检查过文档目录图像的图像数据是否为零?您必须通过正确的路径检索图像并确保您提供正确的位置

答案 3 :(得分:0)

AsyncImageViewUIImageView的子类。您可以使用image加载图片后设置initWithContentsOfFile:filePath属性。您只需要确定要从目录加载哪些图像,以及从Web加载哪些图像。

答案 4 :(得分:0)

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [[_arrayForTable valueForKey:@"URL"] objectAtIndex:[indexPath row]];

NSURL *url = [NSURL URLWithString:path];
if ([path rangeOfString:documentsDirectory].location != NSNotFound)
    url = [NSURL fileURLWithPath:path isDirectory:NO];

AsyncImageView *imageView = [[[AsyncImageView alloc] init] autorelease];
imageView.frame = CGRectMake(0, -5, 45, 45);
imageView.imageURL=url;

答案 5 :(得分:0)

Asynchimageview是否支持从文档目录或带有图像url的包中加载图像?尝试首先从包中加载图像。最近我遇到了类似的问题,最终从服务器本身加载占位符。

与其他一些答案一样,您可以先创建一个UIImage对象,initWithContentsOfFile:设为asynchimageView.image