2012年4月29日日曜日

非同期仮完了

とりあえず画像の読み込みを非同期にするため、UIImageViewのサブクラスを作ってNSURLConnection使って画像をロードする。@implementationの直下に裸で大域変数置いたらインスタンス同士で書き換えるみたいでエラー出まくりでしかも非同期やからdealloc済みにアクセスしてますよーとか配列にそんなに数入ってないですよーとか毎回違うエラーが出てハマった。ちゃんと@interfaceで宣言しとけば大丈夫みたい。基本か。そうだろうな。

Photo.m
#import "Photo.h"
#define nwf(x) [NSNumber numberWithFloat:x]
#define ofv(x,y) [[x objectAtIndex:y]floatValue]

@implementation Photo

- (id)initWithFrame:(CGRect)frame photourl:(NSMutableArray *)cont;
{
    self = [super initWithFrame:frame];
    if (self) {
  photourl=[[NSArray alloc]initWithArray:cont];
  imgnum=0;
  multiFlg=NO;
  if([photourl count]>1){
   multiFlg=YES;
   imgArr=[[NSMutableArray alloc]initWithCapacity:0];
  }
  UIScreen *uis=[UIScreen mainScreen];
  sw=[uis bounds].size.width,sh=[uis bounds].size.height,sr=sw/sh;
    }
    return self;
}

-(void)loadPhoto{
 [self abort];
 if(!ai) ai=[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
 [ai setCenter:self.center];[self addSubview:ai];[ai startAnimating];
 data=[[NSMutableData alloc]initWithCapacity:0];
 NSURLRequest *req=[NSURLRequest 
        requestWithURL:[NSURL URLWithString:[photourl objectAtIndex:imgnum]]
        cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
 conne=[[NSURLConnection alloc]initWithRequest:req delegate:self];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
 [data setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)nsdata{
 [data appendData:nsdata];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
 [self abort];
 NSLog(@"error.");
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
 if(multiFlg){
  UIImage *img=[UIImage imageWithData:data];
  [imgArr addObject:img];
  [imgArr addObject:nwf(img.size.width)];[imgArr addObject:nwf(img.size.height)];[imgArr addObject:nwf(img.size.width/img.size.height)];
  if([imgArr count]/4<[photourl count]){
   imgnum++;[self loadPhoto];
  }
  else{ //tiling routine
   int curw=0,curh=0,k=0;
   for(int j=0;j<[imgArr count]/4;j++){
    if([[imgArr objectAtIndex:j*4+3]floatValue]<sr&&[imgArr count]/4>j+1){
     int over3=2;
     float firstw=ofv(imgArr, j*4+1),firsth=ofv(imgArr, j*4+2),firstr=firsth/firstw;
     float nextw=ofv(imgArr, (j+1)*4+1),nexth=ofv(imgArr, (j+1)*4+2);
     float newh=firstr*((sw*nexth)/(nextw*firstr+nexth));
     while (sw/newh<sr&&[imgArr count]/4>j+over3) {
      nextw=ofv(imgArr, (j+over3)*4+1),nexth=ofv(imgArr, (j+over3)*4+2);
      newh=(newh/sw)*((sw*nexth)/(nextw*(newh/sw)+nexth));
      over3++;
     }
     j+=1+(over3-2);
     UIGraphicsBeginImageContext(CGSizeMake(sw,curh+newh));
     if(mixedimg){[mixedimg drawAtPoint:CGPointMake(0, 0)];}
     while(k<j+1){
      float neww=newh*[[imgArr objectAtIndex:k*4]size].width/[[imgArr objectAtIndex:k*4]size].height;
      [[imgArr objectAtIndex:k*4]drawInRect:(CGRectMake(curw, curh,neww,newh))];
      curw+=neww;k++;
     }
     curh+=newh;curw=0;
     mixedimg=UIGraphicsGetImageFromCurrentImageContext();
     UIGraphicsEndImageContext();
    }else{
     UIGraphicsBeginImageContext(CGSizeMake(sw,curh+sw*[[imgArr objectAtIndex:j*4]size].height/[[imgArr objectAtIndex:j*4]size].width));
     if(mixedimg){[mixedimg drawAtPoint:CGPointMake(0, 0)];}
     float newh=sw*[[imgArr objectAtIndex:j*4]size].height/[[imgArr objectAtIndex:j*4]size].width;
     [[imgArr objectAtIndex:j*4]drawInRect:(CGRectMake(0, curh, sw, newh))];
     mixedimg=UIGraphicsGetImageFromCurrentImageContext();
     UIGraphicsEndImageContext();
     curh+=sw*[[imgArr objectAtIndex:j*4]size].height/[[imgArr objectAtIndex:j*4]size].width;
    }
   }
   [ai removeFromSuperview];
   self.image=mixedimg;
   if(self.image.size.width/self.image.size.height<sw/sh)
    [self setFrame:CGRectMake((sw-sh*self.image.size.width/self.image.size.height)/2, 0, sh*self.image.size.width/self.image.size.height, sh)];
   else [self setFrame:CGRectMake(0, (sh-sw*self.image.size.height/self.image.size.width)/2, sw, sw*self.image.size.height/self.image.size.width)];
   [self abort];
  }
 }
 else{
  [ai removeFromSuperview];
  self.image=[UIImage imageWithData:data];
  if(self.image.size.width/self.image.size.height<sw/sh)
   [self setFrame:CGRectMake((sw-sh*self.image.size.width/self.image.size.height)/2, 0, sh*self.image.size.width/self.image.size.height, sh)];
  else [self setFrame:CGRectMake(0, (sh-sw*self.image.size.height/self.image.size.width)/2, sw, sw*self.image.size.height/self.image.size.width)];
  [self abort];
 }
}

できた。画像のURLが入った配列を引数にして初期化してやればちゃんと読む。複数でも大丈夫。あとはanimatedなgifをどう扱うか。imageIO? UIWebView?ちょっと調べる。

昨日の昼は十六番@長堀橋で十六番ラーメンセット。スープはかなり美味しいが、麺はどうだろう。尾道ラーメンって全部あんな感じなんかな。日清カップヌードル的な、って言ったら怒られるか。でもまた行きたい。
尾道ラーメン 十六番

2012年4月27日金曜日

バージョン管理

仕事が大変でなかなかxcodeで遊べない。
とりあえず画像を非同期取得する前に、バージョン管理の手法を勉強しとく。
このへんを参考?:
Xcode4でアプリのバージョン管理

昨晩は玉造の角力に行ったがなぜか閉まってた。前行った時も閉まってたしやめたんかな。で、急遽日本橋の小洞天へ。ワンタン麺が絶妙な塩分でうまい。今日の昼はまたまた清正で、今月20杯目。

2012年4月26日木曜日

非同期

メソッドの並列処理云々の前に、画像の読み込みを非同期にする必要があるな。
ここらへんを参考に:

[Objective-C][iPhone] 非同期で画像をロードするUIImageView
[iOS SDK]非同期通信で画像をロードする方法について
UIImageをサーバ経由で取得する方法

夜は難波の味仙で担仔麺。おでんみたいなかちかちな煮玉子が入っててなかなか新鮮。程よい辛さでうまかった。

2012年4月25日水曜日

xcode4.3.2

がapp storeからインストールできない。正常終了もできんし、なんか調子悪いなー。
とりあえずdevelopersのサイトからDLしてみる。
それが終わったら並列処理の勉強やな。

夜は麺家 静で中華そば煮玉子にライス。かなりうまい。次はにぼにぼいってみよう。

2012年4月24日火曜日

課題

UIや挙動なんかはなんとなく目処が立った。ちょこちょこ時間かければ完成するだろう。
あとやりたいけどちょっと調べないといけないことを書いとこう。

・GIFはUIWebViewで包む(これは多分やれそう)
・並列処理(パースしながらどうview作るか、view作りながらいつJSONとるか、pvArrをnow起点に前後10posts(?)であふれたやつをどう消すか→nilを代入?)
・JSONの重複回避、since_id算出ルーチン
・bluetoothキーボードでのオペレーション
・大量のfollowee、followerをどう選択させるか(table?)

うーん、結構残ってるな。。。まあ、ひとつひとつ潰していくしか無いな。今まで通り。

並列処理関連は、以下とか参考にしよう:

並列プログラミングガイド
NSOperationQueue スレッドと処理の関係
NSOperation 非並列実行モードと並列実行モードの使い分け

あと、詳解obj-c2.0も。

画面遷移

長押しで逆巻やlikeを出すようにして、デフォルトでは必要最低限のコントローラで遷移させる。ほぼ思惑通りの動きができた。満足。

 ctrlv=[[UIView alloc]initWithFrame:CGRectMake(0, 0, sw, sh)];[self.view addSubview:ctrlv];
 UIImage *nextimg=[UIImage imageNamed:@"next"];UIImage *nextonimg=[UIImage imageNamed:@"next_on"];
 bnext=[UIButton buttonWithType:UIButtonTypeCustom];bnext.alpha=0.7;
 [bnext setImage:nextimg forState:UIControlStateNormal];[bnext setImage:nextonimg forState:UIControlStateHighlighted];
 [bnext setFrame:CGRectMake(sw-nextimg.size.width-10, sh-nextimg.size.height-30, nextimg.size.width,nextimg.size.height)];
 [bnext addTarget:self action:@selector(npost) forControlEvents:UIControlEventTouchUpInside];
 prelong=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(dpl)];
 [bnext addGestureRecognizer:prelong];
 [ctrlv addSubview:bnext];
 UIImage *previmg=[UIImage imageNamed:@"prev"];UIImage *prevonimg=[UIImage imageNamed:@"prev_on"];
 bprev=[UIButton buttonWithType:UIButtonTypeCustom];bprev.alpha=0.7;
 [bprev setImage:previmg forState:UIControlStateNormal];[bprev setImage:prevonimg forState:UIControlStateHighlighted];
 [bprev setFrame:CGRectMake(sw-previmg.size.width-10, sh-previmg.size.height-80, previmg.size.width,previmg.size.height)];
 [bprev addTarget:self action:@selector(ppost) forControlEvents:UIControlEventTouchUpInside];

 UIImage *rblgimg=[UIImage imageNamed:@"reblog"];UIImage *rblgonimg=[UIImage imageNamed:@"reblog_on"];
 brblg=[UIButton buttonWithType:UIButtonTypeCustom];brblg.alpha=0.7;
 [brblg setImage:rblgimg forState:UIControlStateNormal];[brblg setImage:rblgonimg forState:UIControlStateHighlighted];
 [brblg setFrame:CGRectMake(sw-rblgimg.size.width-10-nextimg.size.width-10, sh-rblgimg.size.height-30, rblgimg.size.width,rblgimg.size.height)];
 [brblg addTarget:self action:@selector(rblg) forControlEvents:UIControlEventTouchUpInside];
 rllong=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(chngrl)];
 [brblg addGestureRecognizer:rllong];
 [ctrlv addSubview:brblg];
 UIImage *likeimg=[UIImage imageNamed:@"like"];UIImage *likeonimg=[UIImage imageNamed:@"like_on"];
 blike=[UIButton buttonWithType:UIButtonTypeCustom];blike.alpha=0.7;
 [blike setImage:likeimg forState:UIControlStateNormal];[blike setImage:likeonimg forState:UIControlStateHighlighted];
 [blike setFrame:CGRectMake(sw-likeimg.size.width-10-previmg.size.width-10, sh-likeimg.size.height-30, likeimg.size.width,likeimg.size.height)];
 [blike addTarget:self action:@selector(like) forControlEvents:UIControlEventTouchUpInside];
- (void)npost{
 if([pvlayer viewWithTag:now]==[pvArr lastObject]) return;
 [[pvlayer viewWithTag:now++] removeFromSuperview];
 [pvlayer addSubview:[pvArr objectAtIndex:now-1]];
 [bprev removeFromSuperview];
}
- (void)ppost{
 if(now==1){[bprev removeFromSuperview];return;}
 [[pvlayer viewWithTag:now--] removeFromSuperview];
 [pvlayer addSubview:[pvArr objectAtIndex:now-1]];
 if(now==1)[bprev removeFromSuperview];
}
- (void)dpl{
 if(now!=1) [ctrlv addSubview:bprev];
 else if([bprev isDescendantOfView:ctrlv])[bprev removeFromSuperview];
}
- (void)rblg{
 NSLog(@"reblog.");
}
- (void)like{
 NSLog(@"like.");
}
- (void)chngrl{
 if([brblg isDescendantOfView:ctrlv]){[brblg removeFromSuperview];[ctrlv addSubview:blike];[blike addGestureRecognizer:rllong];}
 else{[blike removeFromSuperview];[ctrlv addSubview:brblg];[brblg addGestureRecognizer:rllong];}
}

できた。でもコードがださいのよねー。素人まるだし。
reblog,likeなどの実装はまだ。でもだいぶ進んだなー。

2012年4月23日月曜日

UI

久々にfireworksをたちあげてインターフェイスの構築。なんとなくイメージは固まってきたかな。macとwindowsの行き来がめんどくさい。
昼は大阪大勝軒@日本橋でつけ麺。並400g中盛800g大盛1500gという量子力学的な麺量設定のため、なかなか中盛に手が出せない。でもうまい。