我有一个应用程序,为我的应用程序上的每张票创建一个独特的照片库.我需要弄清楚如何遍历该库,以便我可以一次一个地将图像上传到服务器.我在youtube上关注了这些教程. (
Video)我目前正在使用Swift作为我的语言.我的最终目标是拥有类似下面的伪代码.
//I need the following for loop example for photo in gallery { uploadToServer(photo) } //this function works already func uploadToServer(photo:UIImage) { callRest postPhoto }
这是我的代码到目前为止无法弄清楚如何循环.他们虽然.
let fetchOptions = PHFetchOptions() fetchOptions.predicate = NSPredicate(format: "title = %@",self.albumName)? let collection:PHFetchResult = PHAssetCollection.fetchAssetCollectionsWithType(.Album,subtype: .Any,options: fetchOptions) if let first_Obj:AnyObject = collection.firstObject{ //found the album self.assetCollection = collection.firstObject as PHAssetCollection self.albumFound = true } else { albumFound = false } var i = collection.count self.photoAssets = PHAsset.fetchAssetsInAssetCollection(self.assetCollection,options: nil) self.photoAssets.enumerateObjectsUsingBlock{(object: AnyObject!,count: Int,stop: UnsafeMutablePointer<ObjCBool>) in if object is PHAsset{ let asset = object as PHAsset println("Inside If object is PHAsset,This is number 1") let imageSize = CGSize(width: asset.pixelWidth,height: asset.pixelHeight) /* For faster performance,and maybe degraded image */ let options = PHImageRequestOptions() options.deliveryMode = .FastFormat imageManager.requestImageForAsset(asset,targetSize: imageSize,contentMode: .AspectFill,options: options,resultHandler: { (image,info)->Void in self.photo = image var defaults: NSUserDefaults = NSUserDefaults.standardUserDefaults() var server = defaults.objectForKey("imageServer") as String! var error: NSError? = nil var imageData = UIImageJPEGRepresentation(self.photo,90) var url = NSURL(string: server) var request = NSMutableURLRequest(URL: url!) request.HTTPMethod = "POST" //request.setValue("multipart/form-data",forHTTPHeaderField: "Content-Type") request.HTTPBody = imageData var response: NSURLResponse? = nil let reply = NSURLConnection.sendSynchronousRequest(request,returningResponse:&response,error:&error) let results = NSString(data:reply!,encoding:NSUTF8StringEncoding) println("API Response: \(results)") /* /* The image is now available to us */ self.sendPhotos(image) println("enum for image,This is number 2")*/ }) } }@H_502_8@
解决方法
所以经过Matt的一些研究和指导,我已经弄明白了.这是如何做到这一点.确保在主线程上执行此操作,因此必须设置options.synchronous = true.如果由于某种原因它没有任何照片的价值.
func getSyncPhotos() { self.albumName = String(self.ticket_id!) let fetchOptions = PHFetchOptions() fetchOptions.predicate = NSPredicate(format: "title = %@",self.albumName)? let collection:PHFetchResult = PHAssetCollection.fetchAssetCollectionsWithType(.Album,options: fetchOptions) if let first_Obj:AnyObject = collection.firstObject{ //found the album self.assetCollection = collection.firstObject as PHAssetCollection self.albumFound = true } else { albumFound = false } var i = collection.count self.photoAssets = PHAsset.fetchAssetsInAssetCollection(self.assetCollection,options: nil) let imageManager = PHCachingImageManager() self.photoAssets.enumerateObjectsUsingBlock{(object: AnyObject!,stop: UnsafeMutablePointer<ObjCBool>) in if object is PHAsset{ let asset = object as PHAsset println("Inside If object is PHAsset,This is number 1") 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 imageManager.requestImageForAsset(asset,resultHandler: { image,info in self.photo = image! /* The image is now available to us */ self.sendPhotos(self.photo) println("enum for image,This is number 2") }) } } } func sendPhotos(uploadImage:UIImage) { var defaults: NSUserDefaults = NSUserDefaults.standardUserDefaults() var server = defaults.objectForKey("imageServer") as String! var error: NSError? = nil var imageData = UIImageJPEGRepresentation(uploadImage,90) var url = NSURL(string: server) var request = NSMutableURLRequest(URL: url!) request.HTTPMethod = "POST" //request.setValue("multipart/form-data",forHTTPHeaderField: "Content-Type") request.HTTPBody = imageData var response: NSURLResponse? = nil let reply = NSURLConnection.sendSynchronousRequest(request,error:&error) let results = NSString(data:reply!,encoding:NSUTF8StringEncoding) println("API Response: \(results)") }@H_502_8@ @H_502_8@ 原文链接:https://www.f2er.com/iOS/332498.html