「URLからUIImageを作りたい?なら、こうすればよくってよ。」

id path = @"http://merrimusings.mu.nu/archives/images/groundhog2.jpg";
NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data cache:NO];

以上!

 いきなりエリカ嬢ですわ。

Creating an UIImage from a URL

 しかも写真付き。
 "UIImage URL"でググったらトップに鎮座してた。ちなみに"土偶ガール"でググるとテン・シー・シーが第3位に鎮座。同士たちよ...

 試しに「iPhoneアプリ開発、その(145)」のrssプロジェクトにぶち込んでみるっす。
 まずはRootViewControllerクラスのtableView:cellForRowAtIndexPath:メソッドでUILabelではなくUIImageViewをUITableViewCellに貼り、NSMutableDictionaryのkeyword_Name要素に画像のURLが入ってるものとして上記の処理を実装。

- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
UIImageView* imageview = [[UIImageView alloc]
initWithFrame:CGRectMake(5,5,
TABLE_ROW_HEIGHT - 10,TABLE_ROW_HEIGHT - 10)];
imageview.tag = keyword_Name;
[cell addSubview:imageview];
}
// 各ラベルへの掲示板の内容設定。
NSMutableDictionary* item = [proxy.bbs objectAtIndex:indexPath.row];
NSURL *url = [NSURL URLWithString:[item objectForKey:keywords[tag]]];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data cache:NO];
UIImageView* imageview = (UILabel*)[cell viewWithTag:keyword_Name];
imageview.image = img;
return cell;
}

 あとはBBSProxyクラスのconnectメソッドのURLを"http://localhost/imageup.php"に変更して

-(void)connect
{


[request setURL:[NSURL URLWithString:@"http://localhost/imageup.php"]];


}

 startElementLocalName:...メソッドで"div"、"b"、"font"、"blockquote"ではなく"img"を見張り<img>タグの属性を取り出せばいい。
 属性というのは以下の赤い文字の部分、下の場合だとsrc、width、heightの3つが設定されている事になる。

<img src="./img_xcc_test/20091023014437.jpg" width="100" height="100" />

 ということで属性なんですが、引数のnb_attributesってのがいかにもって感じなんですな。でもってattributesが(const xmlChar**)なのを見て、これは...と思ったんですよ。で、まあ以下のように出力させてみました。

- (void)startElementLocalName:(const xmlChar*)localname
prefix:(const xmlChar*)prefix
URI:(const xmlChar*)URI
nb_namespaces:(int)nb_namespaces
namespaces:(const xmlChar**)namespaces
nb_attributes:(int)nb_attributes
nb_defaulted:(int)nb_defaulted
attributes:(const xmlChar**)attributes;
{
printf("%s\n", localname);
for (int i = 0; i < nb_attributes; i++) {
printf("\t%s\n", attributes[i]);
}


}

 ごらんの有り様だよ。
meta
http-equiv
(null)

 って、なんでじゃ~と調べたらimageup.php側の記述が間違ってました。<meta>タグは< html>タグの後でないと駄目みたいです。間違い↓

<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<html xmlns="http://www.w3.org/1999/xhtml" lang="ja" xml:lang="ja">

 順序を直して再度チャレンジ!
html
lang
(null)
meta
http-equiv
(null)
head
title


input
name
(null)
(null)
p
img
src
(null)
(null)
img
src
(null)
(null)
img
src
(null)
(null)

 う、う~ん。imgとかsrcとか、それらしいのは出てるんだけど...
 ちゅーことで、やっぱドキュメント読まないと無理かということでstartElementNsSAX2Funcコールバック関数についての説明を読んでみました。

startElementNsSAX2Func

 どうも引数attributesはlocalname/prefix/URI/value/endの5つの要素毎にnb_attributes分あるみたいです。つまり

<img src="./img_xcc_test/20091023014437.jpg" width="100" height="100" />

 というタグだと以下のようにnb_attributesには3つの属性という意味の3が入る所までは正しいんですが

$テン・シー・シー-1

 attributes配列自体の構成は以下のように5つのオフセット情報が3個という5 x 3 = 15個の配列になるわけですわ。

$テン・シー・シー-2

 なので、src属性を取り出すには

const xmlChar *valueBegin = attributes[i*5+3];
const xmlChar *valueEnd = attributes[i*5+4];
NSString* relative_url = [[NSString alloc] initWithBytes:valueBegin
length:(valueEnd - valueBegin) encoding:NSUTF8StringEncoding];

 という処理が必要なわけです。
 で、これで取り出せるURLはimageup.phpからの相対パスなので、これを絶対パスに直すため

NSURL* url = [NSURL URLWithString:relative_url
relativeToURL:[NSURL URLWithString:@"http://localhost/imageup.php"]];

 というふうにするわけです。このurlを使い

[url absoluteString];

 とすることで、絶対パスURL文字列が手に入ることになるんですな。
 この文字列をkeyword_Name要素としてNSMUtableDictionaryに登録する事で~

$テン・シー・シー-3

 出た!
 以下、次回。

------------
サンプルソース:rss8.zip