Iphone UIAlertView与UIActivityIndicator有点矛盾
脱机时,我正在从联机映像下载映像以进行缓存。当我打开应用程序时,图像在UIAlertView弹出之前被完全下载。它完成得不对。我想在下载图像之前使UIAlertView弹出。下面是我的代码Iphone UIAlertView与UIActivityIndicator有点矛盾,iphone,ios,uialertview,uiactivityindicatorview,Iphone,Ios,Uialertview,Uiactivityindicatorview,脱机时,我正在从联机映像下载映像以进行缓存。当我打开应用程序时,图像在UIAlertView弹出之前被完全下载。它完成得不对。我想在下载图像之前使UIAlertView弹出。下面是我的代码 - (void) operatePopupUpdating { myAlert = [[UIAlertView alloc] initWithTitle:@"Updating database.." message:nil delegate:self cancelButtonTitle:nil oth
- (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中,使用OperationUpdate和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];
}
-(void)updateImage{
NSString*snake\u ico\u文件=@“snake\u img\u icn”;
NSString*filePath=[self dataFile:[snake_ico_file stringByAppendingString:@“0.jpg]”;
int max_count=[[self readData]count];
如果(![[NSFileManager defaultManager]文件存在路径:文件路径]){
对于(int i=0;i
如何修复它们并正确工作?只有在图像下载完成后,alertView才会显示。解决此问题的一个解决方案是 创建另一个类ImageDownLoad.h in.h 英寸
@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];
}
}
您的UI不会因此而延迟:)只有在图像下载完成后,alertView才会显示。解决此问题的一个解决方案是 创建另一个类ImageDownLoad.h in.h 英寸
@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];
}
}
您的UI不会因为这样做而延迟:)请编辑您的标题或问题的内容:UIAlertView与UIActivityIndicator有点矛盾。它检查数据库是否更新。我在上面编辑了我的代码。请编辑您的标题或问题的内容:UIAlertView与UIActivityIndicator有点矛盾。它检查数据库是否有updating。我在上面编辑了我的代码。我用这个建议实现,然后我得到SaveData]:未识别的选择器发送到实例。这个方法可以在弹出消息之前等待下载完成吗?@WaiToNZa我编辑过,这应该像[(id)delegate performSelector:@selector(SaveData:inFilePath:)with object:receiveData with object:path];非常感谢您解决此问题。:我使用此建议实施,然后我将获得SaveData]:无法识别的选择器发送到实例。此方法可以在弹出消息之前等待下载完成吗?@WaiToNZa I edited,这应该类似于[(id)delegate performSelector:@selector(SaveData:inFilePath:)withObject:receiveData withObject:path];非常感谢您解决此问题。:D