0

以下のコードは、オンラインからダウンロードしてアプリケーション ドキュメント ディレクトリに書き込んだ画像を表示していません。iPhone のシミュレータ ドキュメント ディレクトリ内を確認したところ、ダウンロードしたファイルが存在します。しかし、画像は表示されません。

を使用してリソース イメージを追加しようとするとUIImage *imgBack = [UIImage imageNamed:@"btn-back.png"];、uiimageview に image.bu が表示されます。オンラインからダウンロードされ、app ディレクトリ内に保存されているイメージにはイメージが表示されません。

Plsは私に知らせて、ありがとう

NSString *workSpacePath=[[self applicationDocumentsDirectory] stringByAppendingPathComponent:theFileName];
NSLog(@"%@ workSpacePath ",workSpacePath);
if ( workSpacePath ){

        //--------------
       UIImage *imgBack = [UIImage imageNamed:@"btn-back.png"];
       imageView1 = [[[UIImageView alloc] initWithFrame:CGRectMake(0,0,20,20)] autorelease];
       [imageView1 setBackgroundColor:[UIColor grayColor]];

       imageView1.image = [UIImage imageWithData:[NSData dataWithContentsOfFile:workSpacePath]];
       [cell addSubview:imageView1];
}
4

2 に答える 2

1

これを試してください、これは私にとって完全に機能しています:

 - (void)viewDidLoad
{
     [super viewDidLoad];

     NSURL *url1 = [NSURL URLWithString:@"http://mobiledevelopertips.com/images/logo-iphone-dev-tips.png"];
    UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL:url1]];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,     NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:@"savedImage.png"];

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

    NSArray *paths1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory1 = [paths1 objectAtIndex:0];
    NSString *workSpacePath = [documentsDirectory1 stringByAppendingPathComponent:@"savedImage.png"]; 
    NSLog(@"%@ workSpacePath ",workSpacePath);

if ( workSpacePath )
     {
        imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(20,20,20,20)];
        [imageView1 setBackgroundColor:[UIColor grayColor]];

        imageView1.image = [UIImage imageWithData:[NSData dataWithContentsOfFile:workSpacePath]];
        [self.view addSubview:imageView1];

    }
}
于 2012-07-04T05:25:21.333 に答える
1

最初にファイルを に変換する代わりにNSData、次を使用できます。

[UIImage imageWithContentsOfFile:filePath]

指定されたパスで直接ロードしimageます。

于 2012-07-04T04:41:40.980 に答える