2013-03-07 43 views
-3

我非常沮喪地從NSDocumentDirectory訪問多個圖像。看看我在這個過程中所做的步驟以及我想從你那裏得到的東西。從NSDocumentDirectory保存和訪問圖像的iOS圖像

我的應用程序可以用下面的代碼下載圖片:

// get the image from the url 
    UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]]]; 

    // Save the image file into document folder 
    NSString *dir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]; 

    // Saving JPG File 
    **NSString *jpefilepath = [dir stringByAppendingPathComponent:[NSString stringWithFormat:@"%dwallpaper.jpg",arc4random()]];** 
    NSData *data1 = [NSData dataWithData:UIImageJPEGRepresentation(image, 1.0f)]; 
            [data1 writeToFile:jpefilepath atomically:YES]; 

有了這個代碼我下載各種影像以獨特的名稱。我用jpefilepath直接存儲在mysql數據庫中。

現在我有問題如何從示例代碼直接訪問本地路徑的圖像。

+0

[?你嘗試過什麼(HTTP:// whathaveyoutried。 COM /) – rckoenes 2013-03-07 13:42:37

回答

0

加載圖像:

UIImage *image = [UIImage imageWithContentsOfFile:jpefilepath]; 

加載數據:

NSData *data = [NSData dataWithContentsOfFile:jpefilepath]; 
0

使用此方法:

UIImage *testImage = [UIImage imageWithContentsOfFile:filePath];