Objective c 如何通过PHImageFileURLKey从iphone设备获取图像
我必须将所有iPhone gallery图像发送到服务器。现在我通过这个函数获取所有图像,并将该图像路径保存在核心数据中 但我的问题是,当我把这个图像路径转换成图像Objective c 如何通过PHImageFileURLKey从iphone设备获取图像,objective-c,swift,cocoa-touch,swift2,swift3,Objective C,Swift,Cocoa Touch,Swift2,Swift3,我必须将所有iPhone gallery图像发送到服务器。现在我通过这个函数获取所有图像,并将该图像路径保存在核心数据中 但我的问题是,当我把这个图像路径转换成图像 并将此图像发送到服务器,它在模拟器中工作正常,但我无法将此imagepath转换为设备中的图像。我无法在iPhone设备中将其转换为图像。请给我任何建议。如果它坠毁了,在哪里?(在调试器中)出现了什么错误?当我将此图像路径转换为图像并将应用程序崩溃展开为如下可选值时,我得到了nil对象。 func getAllImage()
并将此图像发送到服务器,它在模拟器中工作正常,但我无法将此imagepath转换为设备中的图像。我无法在iPhone设备中将其转换为图像。请给我任何建议。如果它坠毁了,在哪里?(在调试器中)出现了什么错误?当我将此图像路径转换为图像并将应用程序崩溃展开为如下可选值时,我得到了nil对象。
func getAllImage() {
let manager = PHImageManager.defaultManager()
self.assetsFetchResult.enumerateObjectsUsingBlock{(object: AnyObject!,
count: Int,
stop: UnsafeMutablePointer<ObjCBool>) in
if object is PHAsset{
let asset = object as! PHAsset
let imageSize = CGSize(width: asset.pixelWidth,
height: asset.pixelHeight)
/* For faster performance, and maybe degraded image */
let options = PHImageRequestOptions()
options.deliveryMode = .FastFormat
options.synchronous = true
manager.requestImageForAsset(asset,
targetSize: imageSize,
contentMode: .AspectFill,
options: options,
resultHandler: {
(image, info) -> Void in
debugPrint(object.localIdentifier)
//object.localIdentifier
if let imageURL : NSURL? = info!["PHImageFileURLKey"] as AnyObject? as? NSURL
{
if imageURL != nil{
// let urlString: String = imageURL!.path!
let urlString: String = (imageURL?.lastPathComponent)!
self.createTask(urlString)
print("this is image path \(info!["PHImageFileURLKey"])")
}
}
}
)
}
}
}
func createTask(imagePath : String) {
let entityDescripition = NSEntityDescription.entityForName("PhotoLibrary", inManagedObjectContext: managedObjectContext)
let task = Tasks(entity: entityDescripition!, insertIntoManagedObjectContext: managedObjectContext)
task.frameid = "1"
task.photopath = imagePath
task.accesstoken = ""
task.photoname = "abc"
task.deliverystatus = "no"
do {
try managedObjectContext.save()
} catch _ {
}
}
func fetchDataFromDB() {
let moc = managedObjectContext
let photoFetch = NSFetchRequest(entityName: "PhotoLibrary")
do {
let searchTerms = CommonUtil.getDataForKey("token")
photoFetch.predicate = NSPredicate(format: "NOT (accesstoken CONTAINS %@)",searchTerms)
let photoArray = try moc.executeFetchRequest(photoFetch)
for result in photoArray
{
let match = result as! NSManagedObject
let keys = Array(match.entity.attributesByName.keys)
let dict = match.dictionaryWithValuesForKeys(keys)
debugPrint(dict)
let filePath : String = String(format: dict["photopath"]! as AnyObject as! String)
self.arrGallery.addObject(filePath)
}
} catch {
fatalError("Failed to fetch employees: \(error)")
}
}