Photo2.m
- (void)makeImage:(NSMutableData *)rdata{
if(multiFlg){
UIImage *img=[UIImage imageWithData:rdata];
[imgArr addObject:rdata];
[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
CGFloat curx=0,cury=0,neww=0,newh=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;
CGFloat firstw=ofv(imgArr,j*4+1),firsth=ofv(imgArr,j*4+2),nextw=ofv(imgArr,(j+1)*4+1),nexth=ofv(imgArr,(j+1)*4+2);
newh=firsth<nexth?firsth:nexth;
CGFloat row=firstw*newh/firsth+nextw*newh/nexth;
while (row/newh<sr&&[imgArr count]/4>j+over3){
nextw=ofv(imgArr,(j+over3)*4+1),nexth=ofv(imgArr,(j+over3)*4+2);
if(nexth<newh){row*=nexth/newh;newh=nexth;}
row+=nextw*newh/nexth;
over3++;
}
j+=1+(over3-2);
if(neww){
if(row<neww){cury*=row/neww;neww=row;}
else newh*=neww/row;
}
else neww=row;
UIGraphicsBeginImageContext(CGSizeMake(neww,cury+newh));
if(mixedimg) [mixedimg drawInRect:CGRectMake(0,0,neww,mixedimg.size.height*neww/mixedimg.size.width)];
int k=1+(j-over3);
while(k<j+1){
UIImage *img=[[UIImage alloc]initWithData:[imgArr objectAtIndex:k*4]];
CGFloat prtw=img.size.width*newh/img.size.height;
if([[photourl objectAtIndex:k]hasSuffix:@"gif"]){
if(!gifArr) gifArr=[[NSMutableArray alloc]initWithCapacity:0];
[gifArr addObject:[imgArr objectAtIndex:k*4]];
[gifArr addObject:nwf(curx)];[gifArr addObject:nwf(cury)];[gifArr addObject:nwf(prtw)];[gifArr addObject:nwf(newh)];
}else [img drawInRect:(CGRectMake(curx,cury,prtw,newh))];
curx+=prtw;k++;
}
cury+=newh;curx=0;
mixedimg=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}else{
UIImage *img=[[UIImage alloc]initWithData:[imgArr objectAtIndex:j*4]];
if(neww){if(img.size.width<neww){
cury*=img.size.width/neww;
if(gifArr){
for(int m=0;m<[gifArr count];m++){
if(m%5)[gifArr replaceObjectAtIndex:m withObject:nwf(ofv(gifArr,m)*img.size.width/neww)];
}
}
neww=img.size.width;
}}
else neww=img.size.width;
newh=img.size.height*neww/img.size.width;
UIGraphicsBeginImageContext(CGSizeMake(neww,cury+newh));
if(mixedimg) [mixedimg drawInRect:CGRectMake(0,0,neww,mixedimg.size.height*neww/mixedimg.size.width)];
if([[photourl objectAtIndex:j]hasSuffix:@"gif"]){
if(!gifArr) gifArr=[[NSMutableArray alloc]initWithCapacity:0];
[gifArr addObject:[imgArr objectAtIndex:j*4]];
[gifArr addObject:nwf(0)];[gifArr addObject:nwf(cury)];[gifArr addObject:nwf(neww)];[gifArr addObject:nwf(newh)];
}else [img drawInRect:(CGRectMake(0,cury,neww,newh))];
mixedimg=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
cury+=newh;
}
}
[ai removeFromSuperview];
self.image=mixedimg;
if(gifArr){
for(int l=0;l<[gifArr count]/5;l++){
CGFloat rect[4]={ofv(gifArr,l*5+1),ofv(gifArr,l*5+2),ofv(gifArr,l*5+3),ofv(gifArr,l*5+4)};
UIImage *gif=[self mkGifWithData:[gifArr objectAtIndex:l*5] rect:rect];
UIImageView *gifv=[[UIImageView alloc]initWithImage:gif];
gifv.frame=self.bounds;gifv.contentMode=UIViewContentModeScaleAspectFit;gifv.autoresizingMask=18;
[self addSubview:gifv];
}
}
[self abort];
}
}
else{
[ai removeFromSuperview];
if([[photourl objectAtIndex:0]hasSuffix:@"gif"]) self.image=(UIImage *)[self mkGifWithData:rdata rect:nil];
else self.image=[UIImage imageWithData:rdata];
[self abort];
}
}
-(UIImage *)mkGifWithData:(NSData *)rdata rect:(CGFloat *)rect{
CGImageSourceRef src=CGImageSourceCreateWithData((__bridge CFDataRef)rdata,NULL);
NSDictionary *prop=[(__bridge NSDictionary*)CGImageSourceCopyProperties(src,NULL) objectForKey:(NSString*)kCGImagePropertyGIFDictionary];
size_t count=CGImageSourceGetCount(src);
NSMutableArray *GIFimages=[NSMutableArray array];
for(size_t i=0;i<count;i++){
CGImageRef image=CGImageSourceCreateImageAtIndex(src,i,NULL);
if(rect){
UIGraphicsBeginImageContext(CGSizeMake(self.image.size.width,self.image.size.height));
[[UIImage imageWithCGImage:image] drawInRect:(CGRectMake(rect[0],rect[1],rect[2],rect[3]))];
UIImage *gif=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[GIFimages addObject:gif];
}else [GIFimages addObject:[UIImage imageWithCGImage:image]];
CGImageRelease(image);
}
NSTimeInterval delay=[[prop objectForKey:(NSString*)kCGImagePropertyGIFDelayTime] doubleValue];
//if (!delay) delay=(1.0f/12.0f)*count;
if (!delay) delay=1.2;
CFRelease(src);
return (UIImage *)[UIImage animatedImageWithImages:GIFimages duration:delay];
}
できた。これであらゆるケースに対応できるはず。
と思ってたらlink,audioでボタンを押すとpostのUIViewControllerがdeallocされてることに気付いた。UIViewの時はいけてた(はず)のに。strongで宣言してviewをNSMutableArrayに入れといても次のpostを同じivarでallocしたらpreviousなpostのivarはdeallocされる。viewが生きてるから気づかんかったけど、まあ当然っちゃ当然の振る舞いなんだろう。無理やりcopyしたりするのもややこしいしメモリも気になるのでボタンをrootのvcからpostのviewに貼ってイベントの受取をrootのvcになるよう変更した。
tManCtrlr.m
- (void)mkSinglePostView{
for(int i=[pvArr count];i<[contArr count];i++){
postv=[[SinglePost2 alloc]init];
[postv constructWithtype:[typeArr objectAtIndex:i] cont:[contArr objectAtIndex:i] bnamet:[bnameArr objectAtIndex:i]];
[naxArr addObject:postv.nax];
if(postv.open){
[openArr insertObject:[postv.open objectAtIndex:1] atIndex:i];
UIImage *opimg,*opimg_on;
if([[postv.open objectAtIndex:0]isEqual:@"link"]){
opimg=[UIImage imageNamed:@"openlink"];opimg_on=[UIImage imageNamed:@"openlink_on"];
}
else if([[postv.open objectAtIndex:0]isEqual:@"audio"]){
opimg=[UIImage imageNamed:@"listen"];opimg_on=[UIImage imageNamed:@"listen_on"];
}
UIButton *open=[UIButton buttonWithType:UIButtonTypeCustom];//move.alpha=0.7;
[open setImage:opimg forState:UIControlStateNormal];[open setImage:opimg_on forState:UIControlStateHighlighted];
[open setFrame:CGRectMake(ofv(postv.open,2),ofv(postv.open,3),opimg.size.width,opimg.size.height)];
[open addTarget:self action:@selector(openPage) forControlEvents:UIControlEventTouchUpInside];
[postv.view addSubview:open];
}
[postv.view setTag:i+1];
[pvArr addObject:postv.view];
}
[pvlayer.view addSubview:[pvArr objectAtIndex:now-1]];[pvlayer.view setFrame:self.view.bounds];
if(!now_all) now_all=[[UILabel alloc]init];[self na];
if(!caption) caption=[[UILabel alloc]init];[self mkCaption];
}
できた。とりあえず顕在しているエラーは潰せたはず。
次は全typeの回転対応とiPad対応してreblog,like処理やってsetting対応してtimelineか。長い。
0 件のコメント:
コメントを投稿