2014-03-06 51 views
1

Im使用this tutorial在我的應用程序中異步加載圖像。我修改了該代碼,以便將圖片保存到iPhone的本地文件,並可以在離線時加載。我想使它在一定的時間間隔(可能15-20秒)之後超時,並加載保存的文件而不是下載新的文件。我找到了使Web視圖超時的方法,但我不知道如何使用異步方法來完成此操作。我怎樣才能讓這段代碼加載url的方式發出超時請求?如何使URL加載請求超時?

感謝

編輯:我想使它超時如果它不能連接到網站,並如果圖片的下載時間過長。

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    NSOperationQueue *queue = [NSOperationQueue new]; 
    NSInvocationOperation *operation = [[NSInvocationOperation alloc] 
             initWithTarget:self 
             selector:@selector(loadImage) 
             object:nil]; 
    [queue addOperation:operation]; 

} 


- (void)loadImage { 
    NSData* imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://www.TestURL.com/test.jpg"]]; 
    UIImage* image = [[UIImage alloc] initWithData:imageData]; 

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *localFilePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"test.jpg"]]; 
    [imageData writeToFile:localFilePath atomically:YES]; 
    [self performSelectorOnMainThread:@selector(displayImage:) withObject:image waitUntilDone:YES]; 


} 
+0

看吧:可能重複:http://stackoverflow.com/questions/2149929/how-to-know-when-nsdatas-initwithcontentsofurl-has-finished-loading – trumpetlicks

回答

3
如果使用

NSData* imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://www.TestURL.jpg"]];您正在創建的同步連接,所以你不容取消。你需要等到最後。

你應該實現使用NSURLConnection異步下載爲How can I deal with connection problem when I use NSData?

+0

鏈接你提供展示如何使連接請求超時,更正?一旦它已經連接到URL,我怎樣才能讓鏈接圖片的下載超時? –

+0

在連接URL之前,您應該在請求中設置超時時間。 'initWithURL:的CachePolicy:timeoutInterval:' –

0

現在解釋說,我是一個新手,在這,我意識到這是一個辦法做到這一點,但真的不回答這個問題,但是我如何處理這種情況是編寫一種方法,首先在本地檢查圖像,如果不存在,則從網絡加載並將其保存在本地,以便下次出現。這是一些代碼。

- (UIImage *)checkForLocalImageThenSave:(NSString *)name fromStringURL:(NSString *)url { 

    NSLog(@"********** Start loading image **********\n\n"); 
    UIImage *image = [[UIImage alloc] init]; 

    NSString *localDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
    NSString *profilePicName = [NSString stringWithFormat:@"/%@", name]; 
    NSString *profilePicNameOnline = [NSString stringWithFormat:@"%@", url]; 

    NSString *directoryWithProfilePicName = [localDirectory stringByAppendingString:profilePicName]; 

    NSLog(@"Looking for file: %@", directoryWithProfilePicName); 
    BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:directoryWithProfilePicName]; 

    if (fileExists) { 

     NSLog(@"File exists. Load: %@\n\n", directoryWithProfilePicName); 
     image = [[UIImage alloc] initWithContentsOfFile: directoryWithProfilePicName]; 

     NSLog(@"********** Loading image done **********\n\n"); 


    } else { 

     NSLog(@"File does not exist. Save: %@", directoryWithProfilePicName); 

     // TO SAVE A JPEG FILE 

     NSData *imageWithURL = [[NSData alloc] initWithContentsOfURL:[[NSURL alloc] initWithString:profilePicNameOnline]]; 

     NSLog(@"File at? %@", profilePicNameOnline); 


     image = [[UIImage alloc] initWithData:imageWithURL]; 
     NSString *jpegFilePath = directoryWithProfilePicName; 
     NSData *data = [NSData dataWithData:UIImageJPEGRepresentation(image, 1.0f)];//1.0f = 100% quality 
     [data writeToFile:jpegFilePath atomically:YES]; 


     NSLog(@"Saving image done."); 

    } 

    return image; 
}