0

我已经使用以下代码将图像保存在文档目录中,并且图像成功保存在文档目录中

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 数组,它包括 URL,一些链接来自 facebook,一些来自文档目录

现在我的问题如下

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

我已检查位置对于图像完全正确,但现在显示

4

6 回答 6

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];

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

于 2013-03-26T10:08:52.567 回答
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");

希望我有所帮助。

于 2013-03-26T08:40:14.673 回答
0

AsyncImageView是 的子类UIImageView。您可以image在使用加载图像后简单地设置属性initWithContentsOfFile:filePath。您只需要弄清楚要从目录中加载哪些图像,以及从网络加载哪些图像。

于 2013-03-26T07:56:23.353 回答
0

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

于 2013-03-26T07:51:24.400 回答
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;
于 2013-03-26T08:26:35.983 回答
0

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

就像在其他一些答案中一样,您可以首先使用 set创建一个UIImage对象。initWithContentsOfFile:asynchimageView.image

于 2013-03-26T08:42:10.157 回答