I have incorporated this framework into my app for selecting images from the camera roll. My app allows users to select one or multiple images and add a timestamp String to the image and save the new the image to the users camera roll. Right now it saves the edited images as a new image but I am trying to figure out how to instead update the existing image in the user's camera roll. I know I should be able to do this using the Photos framework released with iOS 8 but I am really struggling. I watched the 2014 WWDC session "Introducing the Photos Framework" but it is in Objective-C and I am having trouble translating.
Here is my code for selecting the image from the camera roll. I get an array of PHAssets.
func selectImageFromCameraRoll(mediaType: String) {
newMedia = false
let newImagePicker = BSImagePickerViewController()
newImagePicker.doneButton = UIBarButtonItem(title: "Stamp", style: UIBarButtonItemStyle.done, target: self, action: nil)
bs_presentImagePickerController(newImagePicker, animated: true,
select: { (asset: PHAsset) -> Void in
print("Selected")
}, deselect: { (asset: PHAsset) -> Void in
print("Deselected")
}, cancel: { (assets: [PHAsset]) -> Void in
print("Cancel")
}, finish: { (assets: [PHAsset]) -> Void in
for asset in assets {
self.saveAssetToRoll(asset: asset)
}
print("Finished")
}, completion: nil)
}
Here is my code to save the updated image to the camera roll.
func saveAssetToRoll(asset: PHAsset) {
let manager = PHImageManager.default()
let option = PHImageRequestOptions()
var pickedImage = UIImage()
option.isSynchronous = true
option.deliveryMode = .highQualityFormat
manager.requestImage(for: asset, targetSize: PHImageManagerMaximumSize, contentMode: .default, options: option, resultHandler: {(result, info)->Void in
pickedImage = result!})
let width = pickedImage.size.width
let height = pickedImage.size.height
let location = Locations(rawValue: UserDefaults.standard.value(forKey: "location") as! String)!
var point = CGPoint(x: 0, y: 0)
switch location {
case .topLeft:
point = CGPoint(x: 30, y: 50)
case .topRight:
point = CGPoint(x: width - 30, y: 50)
case .bottomLeft:
point = CGPoint(x: 30, y: height - 50)
case .bottomRight:
point = CGPoint(x: width - 30, y: height - 50)
case .center:
point = CGPoint(x: width / 2, y: height / 2)
case .topCenter:
point = CGPoint(x: width / 2, y: 50)
case .bottomCenter:
point = CGPoint(x: width / 2, y: height - 50)
}
let savedFormat = UserDefaults.standard.value(forKey: "format") as! String
var date = Date()
if !currentDateBool {
date = UserDefaults.standard.value(forKey: "selectedDate") as! Date
}
let timestampText = getFormattedDateFromFormatType(formatType: savedFormat, date: date) as NSString
let timestampImage = textToImage(drawText: timestampText, inImage: pickedImage, atPoint: point)
UIImageWriteToSavedPhotosAlbum(timestampImage, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)
}
From reading Apple's documentation it looks like I need to be using a combination of PHContentEditingInput and PHContentEditingOutput. Can someone please point me in the correct direction for incorporating this into my code? Thanks!