gpt4 book ai didi

iphone - UIAlertView 与 UIActivityIndi​​cator 有点矛盾

转载 作者:行者123 更新时间:2023-11-29 13:38:42 25 4
gpt4 key购买 nike

我在离线时从在线图片中下载图片进行缓存。当我打开应用程序时,图像在 UIAlertView 弹出之前就已完全下载。它的完成不正确。我想让 UIAlertView 在图片下载之前弹出。下面是我的代码。

- (void) operatePopupUpdating {
myAlert = [[UIAlertView alloc] initWithTitle:@"Updating database.." message:nil delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
[myAlert show];

UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];

indicator.center = CGPointMake(myAlert.bounds.size.width / 2, myAlert.bounds.size.height - 50);
[indicator startAnimating];
[myAlert addSubview:indicator];

[self operateUpdate];
[self updateImage];

[self operationCompleted];
}

updateImage中的operateUpdate和operationCompleted方法

- (void)updateImage {
NSString *snake_ico_file = @"snake_img_icn_";
NSString *filePath = [self dataFile:[snake_ico_file stringByAppendingString:@"0.jpg"]];
int max_count = [[self readData] count];
if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
for (int i = 0; i < max_count; i++) {
NSString *path = @"http://exitosus.no.de/drngoo/image/";
path = [path stringByAppendingFormat:@"%d",i];
NSString *ico_path = [path stringByAppendingFormat:@"/ico"];

//icon
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filename = [snake_ico_file stringByAppendingFormat:@"%d.jpg"];
NSString *localFilePath = [documentsDirectory stringByAppendingPathComponent:filename];
NSData *thedata = [NSData dataWithContentsOfURL:[NSURL URLWithString:ico_path]];;
[thedata writeToFile:localFilePath atomically:YES];
}
}

-(void) operationCompleted
{
[myAlert dismissWithClickedButtonIndex:0 animated:YES];

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message form System" message:@"Database was updated successful" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];

[alert show];
}

- (void)operateUpdate {

NSString *filePath = [self dataFileSettingPath];
int updatedVer = 0;
int version = [self checkDatabase];
if (version == -1) {
updatedVer = [self createDataBase:0];
} else {
updatedVer = [self updateDataBase:version];
}

[self.settingInfo setObject:[NSString stringWithFormat:@"%d",updatedVer] forKey:@"DBVersion"];
[self.settingInfo writeToFile:filePath atomically:YES];
}

我如何修复它们并正常工作?

最佳答案

只有在图片下载完成后才会显示 alertView。解决此问题的一种解决方案是,

创建另一个类ImageDownLoad.h

在 .h 中

@property(nonatomic,assign)id delegate;
@property(nonatomic,retain) NSString *path;

以.m 为单位

@synthesize delegate;
@synthesize path;

创建方法即

-(void)startDownloadImageWithUrl:(NSURL*)imageUrl withLocalFilePath:(NSSTring*)filePath{
path = filePath;
receiveData = [NSMutableData data];
NSURLRequest *request = [[NSURLRequest alloc]initWithURL:imageUrl];
NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[receiveData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
[(id)delegate performSelector:@selector(SaveData:inFilePath:) withObject:receiveData withObject:path];
}

这将创建连接并开始在单独的类中下载您的图像。然后在您的 mai ViewController 中添加如下代码。

  - (void)updateImage {
NSString *snake_ico_file = @"snake_img_icn_";
NSString *filePath = [self dataFile:[snake_ico_file stringByAppendingString:@"0.jpg"]];
int max_count = [[self readData] count];
if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
for (int i = 0; i < max_count; i++) {
NSString *path = @"http://exitosus.no.de/drngoo/image/";
path = [path stringByAppendingFormat:@"%d",i];
NSString *ico_path = [path stringByAppendingFormat:@"/ico"];

//icon
NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filename = [snake_ico_file stringByAppendingFormat:@"%d.jpg"];
NSString *localFilePath = [documentsDirectory stringByAppendingPathComponent:filename];

ImageDownLoad *imageDown = [[ImageDownLoad alloc]init];
imageDown.delegate = self;
[imageDown startDownloadImageWithUrl:[NSURL URLWithString:ico_path] withLocalFilePath:localFilePath];

}
}

每次图片下载完成都会调用该方法。

   -(void)SaveData:(NSData*)data inFilePath:(NSString*)filePath{

[data writeToFile:filePath atomically:YES];

}

这样做不会延迟您的 UI :)

关于iphone - UIAlertView 与 UIActivityIndi​​cator 有点矛盾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9789218/

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