簡體   English   中英

將圖像保存到Main Bundle,然后將其加載到HTML可以在Simulator上運行,但不能在真實設備上運行嗎?

[英]Saving images to Main Bundle and then loading them into HTML works on Simulator, but not on real Device?

我正在嘗試從html字符串中預加載圖像標簽,以便在設備離線時加載它們。 基本上,我從所有<img>標記中剝離源URL,清理URL以使其具有干凈的文件名,然后下載圖像。

    __block   NSString *imageName = [[NSString alloc]init];

//Get the string after HTTP://
     NSArray *nohttp = [actualUrl componentsSeparatedByString:@"//"];
    imageName = [nohttp objectAtIndex:1];

//Clean any /.:
    imageName = [imageName stringByReplacingOccurrencesOfString:@"/" withString:@""];
    imageName = [imageName stringByReplacingOccurrencesOfString:@"." withString:@""];
    imageName = [imageName stringByReplacingOccurrencesOfString:@":" withString:@""];
//Add .png at the end as we will be saving it as a PNG
    imageName = [NSString stringWithFormat:@"%@.png", imageName];

//change the source url to the new filename of the image so the WebView will load it from the main bundle
    html = [html stringByReplacingOccurrencesOfString:actualUrl withString:imageName];

//save the image to the main bundle
    NSString *pathString = [NSString stringWithFormat:@"%@/%@",[[NSBundle mainBundle]bundlePath], imageName];

//this just checks if the image already exists                             
    BOOL test = [[NSFileManager defaultManager] fileExistsAtPath:pathString];

     if (!test){

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul); dispatch_async(queue, ^(void) {

     NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:actualUrl]];
    UIImage* image = [[UIImage alloc] initWithData:imageData];

                                                                                          NSData *imageData2 = UIImagePNGRepresentation(image);
                                                [imageData2 writeToFile:pathString atomically:YES];


    });
 }

然后,在將html字符串加載到UIWebView之后,它在模擬器上可以完美運行,但是在設備上圖像並未加載。 為什么?

    NSURL *baseUrl = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@",[[NSBundle mainBundle]bundlePath]]];


[self.webView loadHTMLString:self.htmlPage baseURL:baseUrl];

有什么建議,想法嗎? 圖像可以在iOS設備/模擬器上很好地下載,但不會加載到真實設備的WebView中。 它非常適合模擬器。

那是對的。

在iOS上,應用程序的捆綁包是只讀的。 任何將更改保存到捆綁軟件中的嘗試都將在iOS設備上失敗。

在Mac OS上,該捆綁包不是只讀的,但您仍應將其視為只讀。 修改應用程序包是一個壞主意。 如果用戶從應用程序商店更新其應用程序,從備份還原等,則即使在Mac OS上,您保存到捆綁軟件中的更改也將丟失。

該模擬器在Mac OS上運行,並針對Mac OS框架和Mac文件系統構建。

Sim和在設備上運行之間存在相當多的差異。 這是一。

另一個示例:iOS文件系統始終區分大小寫。 在Mac OS上運行的模擬器默認情況下不區分大小寫。 (Mac OS可以從運行不同文件系統的卷中讀取。默認文件系統不區分大小寫,但您可以將其設置為區分大小寫。)

文件“ Foo.txt”是與文件“ foo.txt”不同的文件。 它們都可以存在於同一目錄中。 如果您的文件名為“ Foo.txt”,而您嘗試使用字符串“ foo.txt”加載它,則它將在iOS設備上失敗,但在Mac OS上可以使用。

因此,您應該始終在實際設備上測試您的應用程序。 不要以為如果某些東西在SIM卡上可以正常工作,那對於iOS設備是正確的。 可能不是。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM