2013-03-08 24 views
0

我需要使用我的cordova iOs項目中的WWW/Images文件夾中存在的圖像。我正在使用cordova-2.3.0和iOs 6版本。爲www/Images文件夾中的圖像創建UiImage

我試圖通過下面的代碼得到圖像,但它不適用於WWW/Images文件夾中的圖像,它對資源文件夾中存在的圖像正常工作。

UIImage *image = [UIImage imageNamed:@"Car_Main.png"]; 

如何訪問存在於WWW/Images文件夾中的圖像。

回答

0

我真的不明白,你的WWW/Images文件夾在哪裏。如果在應用程序目錄中 - 試試這個代碼。

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     NSString *documentsDirectory = [paths objectAtIndex:0]; 
     NSString *filePath = [documentsDirectory stringByAppendingString:@"/WWW/Images/Car_Main.png"]; 
     UIImage *img = [[UIImage alloc] initWithContentsOfFile:filePath]; 

如果在項目中 - 也許你忘了添加圖像到目標資源?

0
// Save from documents 

NSString* path = [NSHomeDirectory() stringByAppendingString:@"/WWW/Images/Car_Main.png.png"]; 

BOOL Done = [[NSFileManager defaultManager] createFileAtPath:path 
      contents:nil attributes:nil]; 

if (!Done) { 
    NSLog(@"Error creating file %@", path); 
} else { 
    NSFileHandle* myFileHandle = [NSFileHandle fileHandleForWritingAtPath:path]; 
    [myFileHandle writeData:UIImagePNGRepresentation(yourImage)]; 
    [myFileHandle closeFile]; 
} 


// Get from documents 

NSFileHandle* myFileHandle = [NSFileHandle fileHandleForReadingAtPath:path]; 
UIImage* loadedImage = [UIImage imageWithData:[myFileHandle readDataToEndOfFile]]; 
相關問題