0

嗨,我在从 url 获取图像时遇到问题。我从 Web 服务获取这些 url,然后逐个加载图像,这些图像附加在每个 url 中。

-(void)downloadMyfriendReqImage:(NSIndexPath *)path
{
    if (path.row<[arrayPendingRequests count]) {
        NSString *str=[[arrayPendingRequests objectAtIndex:path.row]valueForKey:@"USERIMAGE"];
        UIImage *img = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:str]]];

        if (img==nil) {
            img=[UIImage imageNamed:@"placeholder.png"];
        }

        [dicPendingReqImages setObject:img forKey:str];
        [tblFriendRequests performSelectorOnMainThread:@selector(reloadData) withObject:@"TAG_29" waitUntilDone:NO];
    }
}

这是我用来加载图像的方式。但问题是一些图像没有得到。img变成空值。但我在android中有相同的应用程序。它完美地加载了所有这些图像。这是什么问题..任何人都可以帮助我。

4

3 回答 3

0

尝试这个:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSString *str=[[arrayPendingRequests objectAtIndex:path.row]valueForKey:@"USERIMAGE"];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:str]];
NSURLResponse *response;
NSError *error;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if (error) {
    // handle error
    return;
}
dispatch_async(dispatch_get_main_queue(), ^{
    // do something with the data
 UIImage *img = [[UIImage alloc] initWithData:data];
});
});
于 2013-07-03T09:12:38.990 回答
0
-(void)downloadMyfriendReqImage:(NSIndexPath *)path
{
    if (path.row<[arrayPendingRequests count]) {
        NSString *str=[[arrayPendingRequests objectAtIndex:path.row]valueForKey:@"USERIMAGE"];
        //ADDED LINE
        str=[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

        UIImage *img = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:str]]];

        if (img==nil) {
            img=[UIImage imageNamed:@"placeholder.png"];
        }

        [dicPendingReqImages setObject:img forKey:str];
        [tblFriendRequests performSelectorOnMainThread:@selector(reloadData) withObject:@"TAG_29" waitUntilDone:NO];
    }
}

我认为您的网址包含一些NSURL无法读取的字符(例如空格),要使其正常工作,您需要为字符串添加编码,我认为它应该以这种方式工作.. 检查//ADDED LINE.

于 2013-07-03T09:01:14.173 回答
0

在将其转换为图像之前检查 NSData 请求的结果。可能是您从服务器获得超时。

于 2013-07-03T08:34:36.613 回答