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?ちょっと調べる。
昨日の昼は十六番@長堀橋で十六番ラーメンセット。スープはかなり美味しいが、麺はどうだろう。尾道ラーメンって全部あんな感じなんかな。日清カップヌードル的な、って言ったら怒られるか。でもまた行きたい。
尾道ラーメン 十六番
0 件のコメント:
コメントを投稿