2015-06-03 60 views
0

下面的代碼適用於iOS < 8很大,但不會在Xcode 6.1.1和iOS 8設備模擬器保存的UIImage與模擬器IOS盤8不起作用

NSData *imageData = UIImagePNGRepresentation(shareSnapshot); 
[imageData writeToFile:@"/Users/MyUser/Desktop/123.png" atomically:YES]; 

有誰工作知道這是一個模擬器問題,我沒有一個真正的設備與ios8檢查。

+0

你沒有一個真正的磁盤路徑。 –

回答

0

所以你在這裏遇到了許可問題。我不知道爲什麼之前8.0的工作,但我看到的(東西清楚地改變):

操作無法完成。 (可可錯誤513)

https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ErrorHandlingCocoa/ErrorObjectsDomains/ErrorObjectsDomains.html#//apple_ref/doc/uid/TP40001806-CH202-CJBGAIBJ

既然這樣,你會在實際設備上運行時遇到問題。處理此問題的更好方法是將數據保存到應用程序的文檔目錄中。

  1. 取通過NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)的目錄路徑
  2. 使用stringByAppendingPathComponent

然後,您可以驗證它是通過訪問模擬器應用程序目錄妥善保存追加文件名:

  • /Users/user/Library/Developer/CoreSimulator/Devices //文檔/
+0

我試過結果是一樣的,下面的IOS 8工作正常,但不是與IOS 8和 –

+0

你當然有權寫入文檔目錄。你可能想建立一個小樣本項目,只是這樣做。隨意將這個示例推送到Github,我很樂意在這方面進行測試。 –

0

我記得由於安全性和沙箱原因,你不能給iOS提供類似「/ Users/MyUser/Desktop /」的路徑。

退房

NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);

文件找到一個合適的位置

NSSearchPathDirectory定義有很多,你可以使用路徑。希望這個幫助

0
UIGraphicsBeginImageContext(self.frame.size); 
[self.layer renderInContext:UIGraphicsGetCurrentContext()]; 

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

// Now I use the "screenshot" 
NSString *path = [NSHomeDirectory() stringByAppendingString:@"/image.jpg"]; 
if ([UIImageJPEGRepresentation(image, 1) writeToFile:path atomically:YES]) { 
    NSLog(@"save ok"); 
} 
else { 
    NSLog(@"save failed"); 
} 
0

試試這個。

雨燕3.0

// Create path. 
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true) 
let filePath = "\(paths[0])/MyImageName.png" 
// Save image. 
UIImagePNGRepresentation(image)?.writeToFile(filePath, atomically: true)