2011-09-09 30 views
0

我正在使用Json解析的應用程序。在json解析的幫助下,我得到了保存在字符串中的照片url。在我的手機顯示圖片我用這個代碼如何將圖像保存在主目錄中?

NSString *strURL=[NSString stringWithFormat:@"%@", [list_photo objectAtIndex:indexPath.row]]; 
NSData *imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: strURL]]; 
CGRect myImage =CGRectMake(13,5,50,50); 
UIImageView *imageView = [[UIImageView alloc] initWithFrame:myImage]; 
[imageView setImage:[UIImage imageWithData: imageData]]; 
[cell addSubview:imageView]; 

現在prblem是,當我回去還是那麼前頁我已經等待幾秒鐘回來的同樣的觀點。現在我希望當我第一次使用應用程序時,我等待那個屏幕,否則從主目錄中獲取圖像。我如何將這些圖像保存在我的主目錄中?如何從主目錄訪問?

+0

看看[這裏](http://stackoverflow.com/questions/2625702/caching-images-in-iphone-app)有幫助 – Saran

回答

0

您可以使用imageData將圖像保存在默認文檔目錄中,如下所示:

// Accessing the documents directory 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:@"myImage.png"]; 

//Writing the image file 
    [imageData writeToFile:savedImagePath atomically:NO]; 
0

您可以使用此文件寫入到您的文檔文件夾

+(BOOL) downloadFileFromURL:(NSString *) url withLocalName:(NSString*) localName 
{ 

    //Get the local file and it's size. 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *finalPath = [documentsDirectory stringByAppendingPathComponent:localName]; 

    NSError *error; 
    NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:finalPath error:&error]; 
    NSAssert (error == nil, ([NSString stringWithFormat:@"Error: %@", error])); 
    if (error) return NO; 
    int localFileSize = [[fileAttributes objectForKey:NSFileSize] intValue]; 


    //Prepare a request for the desired resource. 
    NSMutableURLRequest *request = [NSMutableURLRequest 
            requestWithURL:[NSURL URLWithString:url]]; 
    [request setHTTPMethod:@"HEAD"]; 

    //Send the request for just the HTTP header. 
    NSURLResponse *response; 
    [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 
    NSAssert (error == nil, ([NSString stringWithFormat:@"Error: %@", error])); 
    if (error) return NO; 

    //Check the response code 
    int status = 404; 
    if ([response respondsToSelector:@selector(statusCode)]) 
    { 
     NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*) response; 
     status = [httpResponse statusCode]; 
    } 
    if (status != 200) 
    { 
     //file not found 
     return NO; 
    } 
    else 
    { 
     //file found 
    } 

    //Get the expected file size of the downloaded file 
    int remoteFileSize = [response expectedContentLength]; 


    //If the file isn't already downloaded, download it. 
    if (localFileSize != remoteFileSize || (localFileSize == 0)) 
    {  
     NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]]; 
     [[NSFileManager defaultManager] createFileAtPath:finalPath contents:data attributes:nil]; 
     return YES; 
    } 
    //here we may wish to check the dates or the file contents to ensure they are the same file. 
    //The file is already downloaded 
    return YES; 
} 

這個閱讀:

+(UIImage*) fileAtLocation:(NSString*) docLocation 
{ 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 

    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *finalPath = [documentsDirectory stringByAppendingPathComponent:docLocation]; 


    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]]; 
    [[NSFileManager defaultManager] createFileAtPath:finalPath contents:data attributes:nil]; 

    NSData *databuffer = [[NSFileManager defaultManager] contentsAtPath:finalPath];  
    UIImage *image = [UIImage imageWithData:databuffer]; 
    return image; 

} 
相關問題