簡體   English   中英

從本地下載URL圖像和uitableview單元圖像加載,並在iOS中並行執行。 如何執行?

[英]Downloading url image and uitableview cell image loading from local with parallel execution in iOS. how to implement?

我正在使用sqlite在iOS中存儲圖像本地路徑。 我的過程如下...從服務器返回多個圖像的url路徑。 我將URL圖像轉換為數據(NSData)並將此數據寫入本地路徑(文檔目錄)。(此過程在后台線程中完成)。下一步是將本地路徑(圖像本地路徑)保存到sqlite表中。 我的tableview單元格圖像必須從我的本地路徑圖像(而不是url圖像)加載。當我下載一個url圖像並將圖像路徑保存到sqlite然后從本地路徑加載我的單元格圖像時,我需要這樣。 我的代碼如下

dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
^{
    //Code here is executed asynchronously
    for (int i=0; i<[insertedRowList count]; i++) {

        Messages *mMsg = [insertedRowList objectAtIndex:i];

        NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString: mMsg.msgData]];
        mMsg.msgLocalPath =    [self saveImage:data withFileName:[self getCurrentDateTimeAsNSString] ofType:@"png" inDirectory:documentsDirectoryPath];

        NSData * profData = [NSData dataWithContentsOfURL:[NSURL URLWithString: mMsg.profSerPath]];
        mMsg.profLocPath =  [self saveImage:profData withFileName:[self getCurrentDateTimeAsNSString] ofType:@"png" inDirectory:documentsDirectoryPath];

        BOOL msgsuccess = NO;
        msgsuccess = [[DataBaseManager   getInstance]updatMsgImagePath: mMsg]; //save my image local path to sqlite table

        if (msgsuccess == NO) {

        }
    }
});

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *cellId = @"myMessageCell";
    MessageCell *cell = (MessageCell*)[tableView dequeueReusableCellWithIdentifier:cellId];

    if (cell==nil) {
        cell = [[MessageCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

    Messages *mMsg = [usersArray objectAtIndex:indexPath.row];
    [cell.profImgView setImage:[UIImage imageNamed: mMsg.profLocPath]];  //this path is return from sqlite table.path is like users/……/documents/image.png
    return cell;
}

我建議使用一個庫進行后台下載和圖像緩存,雖然那里有幾個庫,但是我喜歡SDWebImage ,如果設備中已經存在該庫,它將使用緩存的版本。 另一個選擇是AFNetworking

編輯:重新閱讀您的問題,仍然不是100%清楚您需要什么。 據我所知,您正在下載映像並將其保存在設備中,然后將其路徑保存在數據庫中。 我不會將NSData和本地路徑保存在數據庫中,而是將遠程路徑直接保存在數據庫中( mMsg.msgData ),並使用SDWebImage來負責將圖像下載和緩存在磁盤中,您可以使用一些方法像這樣

[cell.imageView setImageWithURL:[NSURL URLWithString:mMsg.msgData]
                   placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

或者可以使用保存在數據庫中的任何路徑來代替mMsg.msgData

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM