URLを使用してimageviewの画像を設定しようとしています
以下のコードを試しました...
NSString *imgURL = [NSString stringWithFormat:@"http://sahhtna.hardtask.org/Files/Clients/thumb/2e10ec86-f323-4889-a996-00cf6758f354.JPG"];
NSLog(@"imageURL===%@", imgURL);
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:imgURL]];
NSLog(@"=====%@", data);
[myImageView setImage:[UIImage imageWithData:data]];
これは完全に機能しています。
ここで、フィードからのデータを使用して URL を試し、以下を試しました。
NSString *imgURL = [NSString stringWithFormat:@"%@", [[feeds objectAtIndex:indexPath.row] objectForKey:@"Image"]];
NSLog(@"imageURL===%@", imgURL);
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:imgURL]];
NSLog(@"=====%@", data);
[myImageView setImage:[UIImage imageWithData:data]];
これにより、以下のような出力が得られます。
2013-08-15 11:22:56.725 Sahhtna[7555:11303] imgURL==http://sahhtna.hardtask.org/Files/Clients/thumb/2e10ec86-f323-4889-a996-00cf6758f354.JPG
2013-08-15 11:22:56.726 Sahhtna[7555:11303] =====(null)
文字列が同じなのに、なぜ (null) としてデータを取得するのか理解できませんか? データが null であるため、UIImageView に画像が表示されません。
何がうまくいかないのですか?
私がそうするNSLog(@"[NSURL URLWithString:imgURL]==%@", [NSURL URLWithString:imgURL]);
と、次のように出力されます[NSURL URLWithString:imgURL]==(null)
理由がわかりました。文字列の最後には、Enter (改行) があります。どうすれば削除できますか?
私は解決策を得ました。
エンタースペースとホワイトスペースがありました。
NSString *imgURL = [NSString stringWithFormat:@"%@", [[feeds objectAtIndex:indexPath.row] objectForKey:@"Image"]];
imgURL = [imgURL stringByReplacingOccurrencesOfString:@"\n" withString:@""];
imgURL = [imgURL stringByReplacingOccurrencesOfString:@" " withString:@""];