0

で使用UITableViewしていCustomCellます。および 2 をCustomCell含む。URLから画像を読み込み、ドキュメント ディレクトリに保存したいと考えています。UIImageViewUILabelUIImageView

ここに私が試した私のコードがあります、

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:[URLarr objectAtIndex:indexPath.row]];//arr is NSMutablearray of image URL

UIImage *img1 = [UIImage imageWithContentsOfFile:savedImagePath];
if (!img1 || [UIImagePNGRepresentation(img1) length] <=0)
{
    id path =[URLarr objectAtIndex:indexPath.row];
    path = [path stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
    NSURL *url = [NSURL URLWithString:path];
    NSMutableArray *arr = [[NSMutableArray alloc] initWithObjects:url, [NSString stringWithFormat:@"%d", indexPath.row+1], nil ];

    [self performSelectorInBackground:@selector(loadImageInBackground:) withObject:arr];

}

そしてこれも。

//—————————————-lazy loading———————–
- (void)loadImageInBackground:(NSArray *)urlAndTagReference
{
        NSData *imgData = [NSData dataWithContentsOfURL:[urlAndTagReference objectAtIndex:0]];
        UIImage *imgload = [[UIImage alloc] initWithData:imgData];

        NSMutableArray *arr = [[NSMutableArray alloc] initWithObjects:imgload, [urlAndTagReference objectAtIndex:1], nil];

        [self performSelectorOnMainThread:@selector(assignImageToImageView:) withObject:arr waitUntilDone:YES];
}

- (void) assignImageToImageView:(NSArray *)imgAndTagReference
{
         NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
         NSString *documentsDirectory = [paths objectAtIndex:0];
         NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:[URLarr objectAtIndex:[[imgAndTagReference objectAtIndex:1] intValue]-1]];

         UIImage* imageToSave = savedImagePath;
         NSData *imageData = UIImagePNGRepresentation(imageToSave);
         [imageData writeToFile:savedImagePath atomically:NO];

}

クラッシュの理由はNSInvalidArgumentExceptionを示しています。また、画像が表示されていませんUITableViewCell。どんな助けでも大歓迎です。

4

2 に答える 2

0

おもう

NSData *imgData = [NSData dataWithContentsOfURL:[urlAndTagReference objectAtIndex:0]];

動かない。代わりにこれを試してください

UIImage *imgload = [UIImage imageWithData:[NSData dataWithContentsOfURL:[urlAndTagReference objectAtIndex:0]]]

これが役立つ場合があります。今すぐこれを試してください。

UIImageView *imageView = [[UIImageView alloc]init];
[imageView_shake_image setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[urlAndTagReference objectAtIndex:0]]]];
UIImage *imgload = [imageView.image retain];

私はわかりません。ちょうど試して

于 2013-05-15T12:54:29.557 に答える
0

テーブル ビュー イメージの遅延読み込みに使用できる適切なオプションはほとんどありません。設計でそれらを利用して時間を節約し、車輪を再発明する努力を避けることができます。
1. Apple 遅延読み込みコード --リンク
2. SDWebImage --リンク

乾杯!アマール。

于 2013-05-15T12:42:29.617 に答える