2014-09-05 58 views
0

我得到無圖像當我試圖從本地文件路徑(緩存目錄) 文件中設置圖像:/// ......的UIImage設置圖像withfile在高速緩存目錄

NSString* [email protected]"file:///Users/mac/Library/Application%20Support/iPhone%20Simulator/7.1/Applications/BD2135B6-5E03-4797-960E-B6C2BF2D6958/Library/Caches/myImage.jpeg" 
[[UIImage alloc]initWithContentsOfFile:localImagePath]; 

我搜索通過許多問題,但無法找到正確的方法來做到這一點。

回答

0

可能的問題是URL編碼的,所以我解決了這個通過獲取緩存目錄路徑

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); 
    NSString *cacheDirectory = [paths objectAtIndex:0]; 
    NSString *localImagePath = [cacheDirectory stringByAppendingPathComponent:@"myImage.jpeg"]; 
    [[UIImage alloc]initWithContentsOfFile:localImagePath]; 
0
- (IBAction)saveImage { 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:@"savedImage.png"]; 
UIImage *image = imageView.image; // imageView is my image from camera 
NSData *imageData = UIImagePNGRepresentation(image); 
[imageData writeToFile:savedImagePath atomically:NO]; 
} 
+0

我不認爲這是有關我的問題 – SandeepAggarwal 2014-09-05 12:49:54