ios 简单的设置图片等比例缩放请求的网络图片

UIScrollView *scroll=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 650, 690)];
scroll.delegate=self;
[bgView addSubview:scroll];
UIImageView *img = [[UIImageView alloc]init];
NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",post,[streetImages objectAtIndex:i-xiaotu_array.count]]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLResponse *urlResponce=nil; NSError *error=nil;
NSData *data=[NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponce error:&error];
UIImage *imageName=[UIImage imageWithData:data];//请求的是网络图片根据判断图的限制高度还是限制宽度进行等比例缩放

if (imageName.size.height >=580) {
[img setFrame:CGRectMake((650-580/imageName.size.height*imageName.size.width)/2, 10, 580/imageName.size.height*imageName.size.width, 580)];
}else{
[img setFrame:CGRectMake(650-imageName.size.width/2, 10, imageName.size.width, imageName.size.height)];
}
[img setImageWithURL:url placeholderImage:nil]; [scroll addSubview:img];
[scroll setContentSize:CGSizeMake(0, 580/imageName.size.width*imageName.size.height+100)];

 

posted on 2013-09-06 10:07  songbai  阅读(1243)  评论(2编辑  收藏  举报