Anysc Image


## 1 Commmon image

[AsyncImageView] (https://github.com/nicklockwood/AsyncImageView)

## 2 UITableView

```objectivec

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    // 再利用できるセルがあれば再利用する
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) {
        // 再利用できない場合は新規で作成
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                      reuseIdentifier:CellIdentifier];
    }
    
    //店舗名
    NSString *tenantName = self.tenantDataSource[indexPath.row][1];
    
    cell.separatorInset = UIEdgeInsetsZero;
    cell.textLabel.text = tenantName;
    cell.textLabel.numberOfLines = 0;
    //    cell.textLabel.textColor = [UIColor colorWithRed:(133/255.0) green:(132/255.0) blue:(132/255.0) alpha:1.0];
    //    cell.textLabel.font = [UIFont fontWithName:@"HiraKakuProN-W6" size:13];
    [cell.textLabel sizeToFit];
    
    cell.tag = indexPath.row;
    
    //非同期による画像読み込み
    NSString* key = self.tenantDataSource[indexPath.row][1];
    __weak typeof(self) weakSelf = self;
    @synchronized (self.imageCasheTenant) {
        if ([weakSelf.imageCasheTenant objectForKey:key] == nil) {
            // リセット
            cell.imageView.image = nil;
            
            // DL
            [weakSelf imageDownloadForTenant:indexPath key:key];
            
        } else {
            
            UIImage *cellImage = [weakSelf.imageCasheTenant objectForKey:key];
            // テーブルからセルを取り出す
            
            CGSize thumb = CGSizeMake(cellTenantImageWidth, cellTenantImageHeigth);
            UIGraphicsBeginImageContextWithOptions(thumb, NO, 0.0);
            [cellImage drawInRect:CGRectMake(0, 0, thumb.width, thumb.height)];
            cellImage = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
            
            cell.imageView.image = cellImage;
        }
    }
    
    
    // カスタムLineHeightを指定
    CGFloat customLineHeight = 18.0f;
    
    // パラグラフスタイルにlineHeightをセット
    NSMutableParagraphStyle *paragrahStyle = [[NSMutableParagraphStyle alloc] init];
    paragrahStyle.minimumLineHeight = customLineHeight;
    paragrahStyle.maximumLineHeight = customLineHeight;
    
    //場所　＆　営業時間
    NSString *tenantDetail = @"";
    tenantDetail = [tenantDetail stringByAppendingString:@"場所："];
    tenantDetail = [tenantDetail stringByAppendingString:self.tenantDataSource[indexPath.row][3]];
    tenantDetail = [tenantDetail stringByAppendingString:@"\n"];
    tenantDetail = [tenantDetail stringByAppendingString:@"営業時間："];
    tenantDetail = [tenantDetail stringByAppendingString:self.tenantDataSource[indexPath.row][4]];
    
    NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:tenantDetail];
    [attributedText addAttribute:NSParagraphStyleAttributeName
                           value:paragrahStyle
                           range:NSMakeRange(0, attributedText.length)];
    
    cell.detailTextLabel.attributedText = attributedText;
    //    cell.detailTextLabel.font = [UIFont fontWithName:@"HiraKakuProN-W6" size:11];
    //    cell.detailTextLabel.textColor = [UIColor colorWithRed:(133/255.0) green:(132/255.0) blue:(132/255.0) alpha:1.0];
    
    cell.detailTextLabel.numberOfLines = 0;
    cell.accessoryType = UITableViewCellAccessoryNone;
    
    return cell;
    
}

- (void) imageDownloadForTenant:(NSIndexPath*)indexPath key:(NSString*)key
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSString *urlStg = self.tenantDataSource[indexPath.row][2];
        UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:urlStg]]];
        
        if(image != nil) {
            __weak typeof(self) weakSelf = self;
            
            @synchronized (weakSelf.imageCasheTenant) {
                [weakSelf.imageCasheTenant setObject:image forKey:key];
            }
            //            dispatch_barrier_sync(_queueCategory, ^(){
            //                [weakSelf.imageCasheCategory setObject:image forKey:key];
            //            });
            
            dispatch_async(dispatch_get_main_queue(), ^{
                UIImage *cellImage = image;
                // テーブルからセルを取り出す
                UITableViewCell* cell = [self.tenantTableView cellForRowAtIndexPath:indexPath];
                
                CGSize thumb = CGSizeMake(cellTenantImageWidth, cellTenantImageHeigth);
                UIGraphicsBeginImageContextWithOptions(thumb, NO, 0.0);
                [cellImage drawInRect:CGRectMake(0, 0, thumb.width, thumb.height)];
                cellImage = UIGraphicsGetImageFromCurrentImageContext();
                UIGraphicsEndImageContext();
                
                cell.imageView.image = cellImage;
                [cell layoutSubviews];
            });
        }
    });
}

```