I have an app that creates a unique photo gallery for each ticket on my app. I need to figure out how to loop through that gallery so I can upload the images to the server one at a time. I followed these tutorials on youtube. (Video) I am currently using Swift as my language. my ultimate goal would be to have something like the following pseudo code.
//I need the following for loop example
for photo in gallery
{
uploadToServer(photo)
}
//this function works already
func uploadToServer(photo:UIImage)
{
callRest
postPhoto
}
here is my code so far cannot figure out how to loop through. them though.
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")*/
})
}
}
requestImageForAsset
is madness. Keep in mind that theresultHandler
is called (1) asynchronously and (2) multiple times for each image. Again, watch the video. They talk all about this. - Please note that your question has now completely morphed; you're asking something completely different now. – Calctufa