在UITableViewCell中下载时显示图像

时间:2015-01-12 17:28:12

标签: ios objective-c image uitableview

我有一个关于下载图像并在UITableViewCell中查看它们的问题。

公开流程:

我登录了该应用。

如果登录有效,它会要求服务器API返回一个JSON数据用户,其中有一个图片ID。

我们通过使用dispantch_async发送图像的ID来请求图像返回API。 此信息和图像存储在NSObject中,该对象存储在NSMutableArray中。

在下载图像时,应用程序传递给第二个UITableViewController,它有一个带图像的自定义UITableViewCell

问题出在这里,当它出现时,图像是空白的,尚未充电。当我刷新数据时,正确显示,因为它们已经被加载。

问题:有没有办法在下载图像时重新充电?无论是一个接一个还是一个接一个。

不幸的是,在Cell上执行图像请求是不可能的,因为这些图像在应用程序的多个表中使用并且想要加载它们一次。

我对我的其他解决方案持开放态度。

非常感谢

问候。

编辑一些代码。

在User:NSObject

中调用下载程序的代码
dispatch_queue_t myQueue = dispatch_queue_create("My Queue",NULL);
dispatch_async(myQueue, ^{
    aux1.imageProfileUrn  = ([itemDic valueForKey:@"imageProfileUrn"] == (id)[NSNull null])? nil : [Imagen getImagenPerfil:[itemDic objectForKey:@"appId"]];;
});

@implementation Imagen +(UIImage *)getImagenPerfil:(NSString *)appID {

UIImage *image;

NSString *cadenaCreacion = [NSString stringWithFormat:@"appID=%@",appID];

NSData *postData = [cadenaCreacion dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/?action=getImagenPerfil",urlApi]]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postData];
[request setTimeoutInterval:100];


NSURLResponse *requestResponse;
NSData *requestHandler = [NSURLConnection sendSynchronousRequest:request returningResponse:&requestResponse error:nil];

NSString *requestReply = [[NSString alloc] initWithBytes:[requestHandler bytes] length:[requestHandler length] encoding:NSUTF8StringEncoding];

NSLog(@"imagen: %@, appid: %@", requestReply, appID);
image = [UIImage imageWithData:requestHandler];
return image;

}

这是每个用户的图片下载程序。

然后在表格中仅将用户图像分配给UIImageView

1 个答案:

答案 0 :(得分:0)

首先添加SDWebImage库...

#import "UIImageView+WebCache.h"

&安培;

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  //Write 
  [cell.Img_thumb sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"Your URl for Image"]] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

    return cell;
}

/// ----------------------第二种方法----------------- -------- //

@carlos,试试这个...在参数发送网址&保存文件名。

-(void) Download_Video :(NSString *)videoUrl tag:(NSString *)filename
{
    //download the file in a seperate thread.
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSLog(@"Downloading Started");
        NSString *urlToDownload = videoUrl;
        NSURL  *url = [NSURL URLWithString:urlToDownload];
        NSData *urlData = [NSData dataWithContentsOfURL:url];
        if ( urlData )
        {
            NSArray       *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            NSString  *documentsDirectory = [paths objectAtIndex:0];

            NSString  *filePath = [NSString stringWithFormat:@"%@/Download/%@.mp4", documentsDirectory,filename];

            //saving is done on main thread
            dispatch_async(dispatch_get_main_queue(), ^{
                [urlData writeToFile:filePath atomically:YES];
                NSLog(@"File Saved !");

                [self.tblMain reloadData];
                UIAlertView *Alert=[[UIAlertView alloc]initWithTitle:@"Download Completed" message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
                [Alert show];
            });
        }
    });
}

<强> &安培;在TABLEVIEW方法中使用以下代码,......

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString  *path = [NSString stringWithFormat:@"%@/Download/%@.mp4", documentsDirectory,[NSString stringWithFormat:@"%d",indexPath.row]];
    BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:path];
    if (fileExists)
    {
        [cell.Btn_Download setImage:[UIImage imageNamed:@"done.png"] forState:UIControlStateNormal];
    }
    else
    {
        [cell.Btn_Download setImage:[UIImage imageNamed:@"download.png"] forState:UIControlStateNormal];
        }
}
相关问题