Iphone 目标-C从Web问题下载图像
我使用以下代码从服务器下载图像:Iphone 目标-C从Web问题下载图像,iphone,objective-c,ios,image,download,Iphone,Objective C,Ios,Image,Download,我使用以下代码从服务器下载图像: - (void) loadDataWithOperation { //Connection test NSURL *url = [NSURL URLWithString:@"http://myurl.com/testconnection.php"]; NSError* error; NSString* connected = [NSString stringWithContentsOfURL:url encoding:NSASCIIStringEncodin
- (void) loadDataWithOperation {
//Connection test
NSURL *url = [NSURL URLWithString:@"http://myurl.com/testconnection.php"];
NSError* error;
NSString* connected = [NSString stringWithContentsOfURL:url encoding:NSASCIIStringEncoding error:&error];
//If the string Connected has NOT manged to initialise itself with the contents of the URL:
if (connected == NULL) {
//Display error picture:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error!" message:@"Error downloading gallery, please check network connection and try again." delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
[alert release];
[self createBackButton];
} else {
//Load Data
NSLog(@"Connected - %@",connected);
NSLog(@"loadDataWithOperartion");
//Create an array to hold the URLS
NSMutableArray *myURLS;
//Initialize the array with nil
myURLS = [[NSMutableArray alloc] init];
NSLog(@"Here1");
//Add all the URLs from the server to the array
for (int i = 0; i <= 4; i++){
NSString *tempString = [[NSString alloc] initWithFormat : @"http://myurl.com/GalleryImages/%djack1.jpg", i];
[myURLS addObject: [NSURL URLWithString:tempString]];
[tempString release];
}
//Array to hold the image data
NSMutableArray *myData;
//Initialize the array with nil
myData = [[NSMutableArray alloc] init];
NSLog(@"Here2");
//Add all the URLs from the server to the array
for (int i = 0; i <= 4; i++){
[myData addObject: [[NSData alloc] initWithContentsOfURL: [myURLS objectAtIndex:i]]];
}
//Array to hold the image data
NSMutableArray *myImages;
//Initialize the array with nil
myImages = [[NSMutableArray alloc] init];
NSLog(@"Here3");
//Add all the URLs from the server to the array
for (int i = 0; i <= 4; i++){
[myImages addObject: [UIImage imageWithData: [myData objectAtIndex:i]]];
}
// Load an array of images into the page view controller
//Initialising them with the data stored above
NSArray *array = [[NSArray alloc] initWithArray:myImages];
[self setImages:array];
//Release the image data
[myURLS release];
[myData release];
[myImages release];
[array release];
}
}
-(void)loadDataWithOperation{
//连接测试
NSURL*url=[NSURL URLWithString:@”http://myurl.com/testconnection.php"];
n错误*错误;
NSString*connected=[NSString stringWithContentsOfURL:url编码:NSASCISTRINGEncoding错误:&错误];
//如果连接的字符串未使用URL的内容初始化自身:
if(connected==NULL){
//显示错误图片:
UIAlertView*alert=[[UIAlertView alloc]initWithTitle:@“错误!”消息:@“下载多媒体资料时出错,请检查网络连接并重试。”代理:自取消按钮:@“确定”其他按钮:无];
[警报显示];
[警报发布];
[自创建BackButton];
}否则{
//加载数据
NSLog(@“已连接-%@”,已连接);
NSLog(@“LoadDataWithOperation”);
//创建一个数组来保存URL
NSMutableArray*myurl;
//用nil初始化数组
myURL=[[NSMutableArray alloc]init];
NSLog(“此处1”);
//将服务器中的所有URL添加到阵列中
对于(int i=0;i在后台下载图像:
-(void)imageDownloadStart
{
[self performSelectorInBackground:@selector(downloadImages) withObject:nil];
}
-(void)downloadImages
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
//WRITE YOU DOWNLOADING CODE HERE
if(yourCondition)
{
[self performSelectorOnMainThread:@selector(imageDownloadStart) withObject:nil waitUntilDone:YES];
}
[pool release];
}
以及调整大小:
-(UIImage *)resizeImage:(UIImage *)image withSize:(CGSize)newSize
{
float actualHeight = image.size.height;
float actualWidth = image.size.width;
float imgRatio = actualWidth/actualHeight;
float maxRatio = newSize.width/newSize.height;
if(imgRatio!=maxRatio)
{
if(imgRatio < maxRatio){
imgRatio = newSize.width / actualHeight;
actualWidth = round(imgRatio * actualWidth);
actualHeight = newSize.width;
}
else{
imgRatio = newSize.height / actualWidth;
actualHeight = round(imgRatio * actualHeight);
actualWidth = newSize.height;
}
}
CGRect rect = CGRectMake(0.0, 0.0, actualWidth, actualHeight);
UIGraphicsBeginImageContext(rect.size);
[image drawInRect:rect];
UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//[resizedImage release];
return resizedImage;
}
-(UIImage*)大小图像:(UIImage*)图像大小:(CGSize)新闻大小
{
浮动实际高度=image.size.height;
浮动实际宽度=image.size.width;
浮动高度=实际宽度/实际高度;
float maxRatio=newSize.width/newSize.height;
如果(imgRatio!=最大比率)
{
if(imgRatio
然而,我的问题是,如果我是
要增加要显示的图像数量,请执行以下操作:
比如说,20,会有很长的一段时间
正在下载图像,请稍候,然后重试
然后显示
NSURLConnection
只允许同时连接6个服务器。您应该使用NSOperationQueue
并调用setMaxConcurrentOperationCount
将值设置为小于6
库ASIHTTPRequest
通过最多4个并发操作实现此方法(以防代码中的其他内容发出请求)。请参阅中的示例