我正在从Gallery
中通过UIImagepicker保存数据库返回路径 let imageURL = info[UIImagePickerControllerReferenceURL] as! NSURL
let imageName = imageURL.lastPathComponent
let documentDirectory = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first! as String
//let localPath = documentDirectory.stringByAppendingPathComponent(imageName!)
let imagePath = String("\(documentDirectory)/\(imageName!)")
路径看起来像
/Users/kshitijghule/Library/Developer/CoreSimulator/Devices/3AC7B500-E980-4A00-BC32-34FA3C7DDE8A/data/Containers/Data/Application/838FB5A1-4448-46C9-8BEC-2E939668B97D/Documents/asset.JPG < / p>
现在我想从此路径加载此图片。
i used this code
let url = NSURL(string:"/Users/kshitijghule/Library/Developer/CoreSimulator/Devices/3AC7B500-E980-4A00-BC32-34FA3C7DDE8A/data/Containers/Data/Application/838FB5A1-4448-46C9-8BEC-2E939668B97D/Documents/asset.JPG")!//path from database but mention static
let imageData = NSData(contentsOfURL:url)!
cell?.petImageView.image = UIImage(data: imageData)
但我在线上崩溃了
let imageData = NSData(contentsOfURL:url)!
如何解决此问题?它可以在设备上运行吗?
答案 0 :(得分:0)
如果要将图像存储在文档目录中,则只存储数据库中的图像名称
在检索时,使用您在保存时完成的图像名称附加当前文档目录路径
import java.io.File;
import java.util.Scanner;
/**
* This is some comment.
* @author John Doe
*/
public class Prog3a {
/**
* The main method executes and runs the program.
* @param args takes in vargs from the CLI
*/
public static void main(String[] args) {
System.out.println("Program 3, John Doe");
if(args.length != 2) {
System.err.println("Usage: java prog3a infilename searchword");
return;
}
try {
final Scanner scan = new Scanner(new File(args[0]));
System.out.println("Opened file " + args[0] + " to search for '" + args[1] + "'");
scan.close();
} catch(java.io.FileNotFoundException e) { // need to make this valid
System.err.println("File not found: " + args[1]);
}
}
}
因为如果发现模拟器的应用程序编号(838FB5A1-4448-46C9-8BEC-2E939668B97D)是导致问题的更改
,我最后也会遇到此类问题希望这可以解决您的问题
答案 1 :(得分:0)
我已经回答了类似的问题here。您只需要将这些路径保存到数据库中,然后从数据库路径中检索这些图像。
答案 2 :(得分:0)
将图像保存在从图库或相机中获取的文档目录中。
\d{5}
从数据库加载图像
func origionalImage(origionalImage: UIImage!, editedImage: UIImage!, userInfo info: [NSObject : AnyObject]!) {
let imageData = NSData(data:UIImagePNGRepresentation(origionalImage)!)
let paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)
let docs: String = paths[0]
let imageName = self.randomStringWithLength(6) as String
//imageName can save in sqlite to load image later
let fullPath = String("\(docs)/\(imageName).png")
print(fullPath)
let result = imageData.writeToFile(fullPath, atomically: true)
print("FILE WRITTEN SUCCESS=>:\(result)")
}