简体   繁体   中英

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

I am using sqlite for storing image local path in iOS. My procedure is as follows… From server returns more than one url path of images . I convert url image to data(NSData) and this data write to my local path(documents directory).(This process done in background thread).Next step is save my local path(image local path) to sqlite table. My tableview cell image must be load from my local path image(not url image).I need like this when one url image downloaded and save image path to sqlite then load my cell image from local path . How to implement this procedure in iOS?My code is following

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

I'd recommend using a library for background downloading and caching of images, there are several out there but I like SDWebImage , this library will use the cached version if it already exist in the device. Another option is AFNetworking .

EDIT: Re-read your question, still not 100% clear what you need. From what I see, you are downloading the image and saving it in device and then saving it's path in your DB; I wouldn't save the NSData and the local path in your DB, I would save the remote path directly in the DB ( mMsg.msgData ) and use SDWebImage to take care of downloading and caching the images in the disk, you could use something like this

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

or instead of mMsg.msgData you can use your whatever path you saved in your DB.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM