gpt4 book ai didi

iphone - Objective - C 从网络下载图片问题

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:09:35 26 4
gpt4 key购买 nike

我有这段代码可用于从服务器下载图像:

- (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];
}

}

此代码有效,因为服务器上总是有一定数量的图像可供下载,在本例中为 4。

但是我的问题是,如果我要增加图像的数量,比如说 20,那么在图像下载然后显示时会等待很长时间。

基本上我想在加载屏幕上下载 5 张图片,然后下载剩余的 15 张图片,同时用户可以查看前 5 张图片。任何人都可以告诉我如何执行此操作吗?

当下载图片时,它们会缩放到 iPhone 屏幕的大小并放置在 UIScrollView 中以供查看。

谢谢,

jack

最佳答案

在后台下载图片:

-(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;
}

关于iphone - Objective - C 从网络下载图片问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6372062/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com