iOS 9 save gif to photo library - ios

Now that AssetsLibrary has been deprecated, we're supposed to use the photos framework, specifically PHPhotoLibrary to save images and videos to a users camera roll.
Using ReactiveCocoa, such a request would look like:
func saveImageAsAsset(url: NSURL) -> SignalProducer<String, NSError> {
return SignalProducer { observer, disposable in
var imageIdentifier: String?
PHPhotoLibrary.sharedPhotoLibrary().performChanges({
let changeRequest = PHAssetChangeRequest.creationRequestForAssetFromImageAtFileURL(url)
let placeholder = changeRequest?.placeholderForCreatedAsset
imageIdentifier = placeholder?.localIdentifier
}, completionHandler: { success, error in
if let identifier = imageIdentifier where success {
observer.sendNext(identifier)
} else if let error = error {
observer.sendFailed(error)
return
}
observer.sendCompleted()
})
}
}
I created a gif from a video using Regift and I can verify that the gif exists inside my temporary directory. However when I go save that gif to the camera roll, I get a mysterious error: NSCocoaErrorDomain -1 (null), which is really super helpful.
Has anyone ever experienced this issue?

You can try this.
let data = try? Data(contentsOf: /*Your-File-URL-Path*/)
PHPhotoLibrary.shared().performChanges({
PHAssetCreationRequest.forAsset().addResource(with: .photo, data: data!, options: nil)
})

Related

Capture Filtered Image with GPUImage and Save to Photo Library with Metadata

I am developing a simple came app for iOS 10. I need to live filter a camera feed, and then capture and save the filtered image.
Using GPUImage, I have been able to setup the live feed and the image capture using GPUImageStillCamera. I am also using the PHPhotoLibrary APIs:
func saveToPhotoLibrary(data: Data, completion: #escaping (PHAsset?) -> ()) {
var assetIdentifier: String?
PHPhotoLibrary.requestAuthorization { (status) in
if status == .authorized {
PHPhotoLibrary.shared().performChanges({
let creationRequest = PHAssetCreationRequest.forAsset()
let placeholder = creationRequest.placeholderForCreatedAsset
creationRequest.addResource(with: .photo, data: data, options: .none)
assetIdentifier = placeholder?.localIdentifier
}, completionHandler: { (success, error) in
if let error = error {
print("There was an error saving to the photo library: \(error)")
}
var asset: PHAsset? = .none
if let assetIdentifier = assetIdentifier {
asset = PHAsset.fetchAssets(withLocalIdentifiers: [assetIdentifier], options: .none).firstObject
}
completion(asset)
})
} else {
print("Need authorisation to write to the photo library")
completion(.none)
}
}
}
The problem is that when this image gets saved, the metadata (such as camera and device information) is missing. How can I save this image with the filter, yet still retain the image metadata?

How to get URL from UIImage?

I have an iOS app in which there are 2 ways the user can get a picture:
Select it from photos library (UIImagePickerController)
Click it from a custom made camera
Here is my code for clicking the image from a custom camera (this is within a custom class called Camera, which is a subclass of UIView)
func clickPicture(completion:#escaping (UIImage) -> Void) {
guard let videoConnection = stillImageOutput?.connection(withMediaType: AVMediaTypeVideo) else { return }
videoConnection.videoOrientation = .portrait
stillImageOutput?.captureStillImageAsynchronously(from: videoConnection, completionHandler: { (sampleBuffer, error) -> Void in
guard let buffer = sampleBuffer else { return }
let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(buffer)
let dataProvider = CGDataProvider(data: imageData! as CFData)
let cgImageRef = CGImage(jpegDataProviderSource: dataProvider!, decode: nil, shouldInterpolate: true, intent: .defaultIntent)
let image = UIImage(cgImage: cgImageRef!, scale: 1, orientation: .right)
completion(image)
})
}
Here is how I click the image within the ViewController:
#IBAction func clickImage(_ sender: AnyObject) {
cameraView.clickPicture { (image) in
//use "image" variable
}
}
Later, I attempt to upload this picture to the user's iCloud account using CloudKit. However I receive an error saying the record is too large. I then came across this SO post, which says to use a CKAsset. However, the only constructor for a CKAsset requires a URL.
Is there a generic way I can get a URL from any UIImage? Otherwise, how can get a URL from the image I clicked using my custom camera (I have seen other posts about getting a url from a UIImagePickerController)? Thanks!
CKAsset represents some external file (image, video, binary data and etc). This is why it requires URL as init parameter.
In your case I would recommend to use following steps to upload large image to CloudKit:
Save UIImage to local storage (e.g. documents directory).
Initialize CKAsset with path to image in local storage.
Upload asset to Cloud.
Delete image from local storage when uploading completed.
Here is some code:
// Save image.
let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
let filePath = "\(path)/MyImageName.jpg"
UIImageJPEGRepresentation(image, 1)!.writeToFile(filePath, atomically: true)
let asset = CKAsset(fileURL: NSURL(fileURLWithPath: filePath)!)
// Upload asset here.
// Delete image.
do {
try FileManager.default.removeItem(atPath: filePath)
} catch {
print(error)
}

Get Image location from UIImagePickerController

I have tried multiple ways to get the location of an image which took through the UIImagePickerController Camera.
What I want to achieve is that, I want to select an image using UIImagePickerController Camera and I have to save it into Photo Library so that only I can take back the PHAsset from it and also the location associated with it.
//MARK: Saving an Image to PhotoLibrary and taking back the PHAsset
class func savingThis(image : UIImage, completion : (asset : PHAsset?) -> ())
{
var localIdentifier : String?
let imageManager = PHPhotoLibrary.sharedPhotoLibrary()
imageManager.performChanges({ () -> Void in
let request = PHAssetChangeRequest.creationRequestForAssetFromImage(image)
if let properAsset = request.placeholderForCreatedAsset {
localIdentifier = properAsset.localIdentifier
}
else {
completion(asset: nil)
}
}, completionHandler: { (success, error) -> Void in
if let properLocalIdentifier = localIdentifier {
let result = PHAsset.fetchAssetsWithLocalIdentifiers([properLocalIdentifier], options: nil)
if result.count > 0 {
completion(asset: result[0] as? PHAsset)
}
else {
completion(asset: nil)
}
}
else {
completion(asset: nil)
}
})
}
I have tried this code, to save and get back the PHAsset. But the problem is that this PHAsset does not have any location associated with it, wondering why? And what I missed?
I believe that I don't have to manually set GPS data into image's metadata right? I think that Photos Framework or Asset Library takes care of it. So as you know that Asset Library is deprecated our only option is to use Photos Framework. I read online that, Saving image to Photo Library takes care of it. Isn't it correct?
Is there any alternative? Should I use UIImageWriteToSavedPhotosAlbum method to save image to Camera Roll, And I can take back the very recent photo using Photos Framework. But I don't think that UIImageWriteToSavedPhotosAlbum will take care of the location thing.
Do you have any thoughts?
First of all thanking all who were all kind to take a look into the question.
I found my answer.
//MARK: Saving an Image to PhotoLibrary and taking back the PHAsset
class func savingThis(image : UIImage, completion : (asset : PHAsset?) -> ())
{
var localIdentifier : String?
let imageManager = PHPhotoLibrary.sharedPhotoLibrary()
imageManager.performChanges({ () -> Void in
let request = PHAssetChangeRequest.creationRequestForAssetFromImage(image)
request.location = // Assigned current location here :)
if let properAsset = request.placeholderForCreatedAsset {
localIdentifier = properAsset.localIdentifier
}
else {
completion(asset: nil)
}
}, completionHandler: { (success, error) -> Void in
if let properLocalIdentifier = localIdentifier {
let result = PHAsset.fetchAssetsWithLocalIdentifiers([properLocalIdentifier], options: nil)
if result.count > 0 {
completion(asset: let asset = result[0] as? PHAsset)
}
else {
completion(asset: nil)
}
}
else {
completion(asset: nil)
}
})
}
When you get a callback from UIImagePickerController:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo
info: [UIImagePickerController.InfoKey : Any]) {}
you can try to get the image location from the info dictionary.
For that, you need to request the related PHAsset object:
if let assetImage = info[UIImagePickerController.InfoKey.phAsset] as? PHAsset {
print("Image location info = \( assetImage.location))")
}
Important - before using this approach you need to request the user permission:
PHPhotoLibrary.requestAuthorization()
If you do not do it the info dictionary for UIImagePickerController.InfoKey.phAsset will return nil.

Using Firebase Storage's putFile() method is resulting in: The file “fileName” couldn’t be opened error

Here are two ways I've tried to upload the file:
1.
getURLOfPhoto(assetURL: imagesDictionary[String(whichProfileImage)]! , completionHandler: { (responseURL) in
FIRStorage.storage().reference().putFile(responseURL as! URL)
})
2.
let assets = PHAsset.fetchAssets(withALAssetURLs: [imagesDictionary[String(whichProfileImage)] as! URL], options: nil)
let asset = assets.firstObject
asset?.requestContentEditingInput(with: nil, completionHandler: { (contentEditingInput, info) in
let imageFile = contentEditingInput?.fullSizeImageURL?
FIRStorage.storage().reference().child("test").putFile(imageFile!, metadata: nil) { (metadata, error) in
if let error = error {
return
}
}
})
I am getting this error:
Body file is unreachable: /var/mobile/Media/DCIM/100APPLE/picture.JPG
Error Domain=NSCocoaErrorDomain Code=257 "The file “picture.JPG” couldn’t be opened because you don’t have permission to view it."
UserInfo={NSURL=file:///var/mobile/Media/DCIM/100APPLE/picture.JPG, NSFilePath=/var/mobile/Media/DCIM/100APPLE/picture.JPG,
NSUnderlyingError=0x15da49a0 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}}
The URL seems to be being retrieved successfully and the error only occurs when the putFile() method gets called.
Does anyone know how to fix this error or another way of uploading a file (not a Data object) to Firebase Storage?
Thanks in advance
Currently Firebase Storage is unable to use file URLs that are retrieved using the PHAsset based code I used in my question (or at least it was't able to in my experience) - even if those files are files the user took with the camera on their own iPhone. So, one solution is to re-save the file in question to a location which is accessible to the Firebase Storage API and then upload the file by passing in that location's URL in to the putFile() method.
You can use this method if you're using the imagePickerController() method:
do {
let documentsURL = FileManager.default().urlsForDirectory(.documentDirectory,
inDomains: .userDomainMask)[0]
let fileURL = try documentsURL.appendingPathComponent("fileName.jpg")
let image = info[UIImagePickerControllerOriginalImage]
try UIImageJPEGRepresentation(image as! UIImage,1.0)?.write(to: fileURL, options: [])
FIRStorage.storage().reference().child("exampleLocation")
.putFile(fileURL, metadata: nil) { (metadata, error) in
if let error = error {
print("Error uploading: \(error.description)")
return
}
}
}
catch {
print("error is ", error)
}
It's possible that our uploader doesn't have the correct permissions to access that file due to the app sandbox (and we're pretty hesitant to grant broad file system access permissions).
I only recommend storing files in Documents/ and tmp/ per https://developer.apple.com/library/ios/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html
Granted, if it's coming from system resources, we might want to revisit that behavior. Typically I just do (yes, I know it's data instead of file and thus will have worse memory behavior):
// pragma mark - UIImagePickerDelegate overrides
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
// Get local image
guard let image: UIImage = info[UIImagePickerControllerOriginalImage] as? UIImage else { return }
let imageData = UIImagePNGRepresentation(image)!
// Get a reference to the location where we'll store our photos
let photosRef = storage.reference().child("chat_photos")
// Get a reference to store the file at chat_photos/<FILENAME>
let photoRef = photosRef.child("\(NSUUID().UUIDString).png")
// Upload file to Firebase Storage
let metadata = FIRStorageMetadata()
metadata.contentType = "image/png"
photoRef.putData(imageData, metadata: metadata).observeStatus(.Success) { (snapshot) in
// When the image has successfully uploaded, we get it's download URL
let text = snapshot.metadata?.downloadURL()?.absoluteString
}
// Clean up picker
dismissViewControllerAnimated(true, completion: nil)
}
#Mike McDonald, thanks for your answer it worked for me. I was having the exact same issue and was able to solve with your suggestions. Here is my code:
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
guard let image: UIImage = info[UIImagePickerControllerOriginalImage] as! UIImage else { return }
let profileImageName = "profileImageName.png"
let imageData = UIImagePNGRepresentation(image)!
let filePath = "\(FIRAuth.auth()!.currentUser!.uid)/\(Int(NSDate.timeIntervalSinceReferenceDate() * 1000))"
let photoStorageRef = FIRStorage.storage().reference().child(filePath)
let photoRef = photoStorageRef.child("\(profileImageName)")
let metadata = FIRStorageMetadata()
metadata.contentType = "image/png"
photoRef.putData(imageData, metadata: metadata) { metadata, error in
if let error = error {
print("Error uploading:\(error.localizedDescription)")
return
} else {
guard let downloadURL = metadata!.downloadURL() else { return }
guard let downloadURLString = metadata!.downloadURL()?.absoluteString else { return }
//do what I need to do with downloadURL
//do what I need to do with downloadURLString
}
}
Hope this can help anyone else having the same issue!

Saving a photo with a name, or any kind of metadata

I've got a completed app that takes photos and puts them in custom albums. I can name each album and I can retrieve all the images perfectly. However what I really need is to be able to name the individual photos (or use some kind of metadata) so that I can show them at appropriate times inside the app.
I know it can be done if you are storing the photos in the app's documents directory but I've had to move away from that and go with the device's photo library.
Has anyone got any ideas around how to do this?
PS. I am using Objective-C not SWIFT.
You can do this in two ways:
1- Saving the photo in a temporary directory. Example:
var fileManager = NSFileManager()
var tmpDir = NSTemporaryDirectory()
let filename = "YourImageName.png"
let path = tmpDir.stringByAppendingPathComponent(filename)
var error: NSError?
let imageData = UIImagePNGRepresentation(YourImageView.image)
fileManager.removeItemAtPath(path, error: nil)
println(NSURL(fileURLWithPath: path))
if(imageData.writeToFile(path,atomically: true)){
println("Image saved.")
}else{
println("Image not saved.")
}
2- Saving using Photos Framework. Example:
if let image: UIImage = YourImageView.image
let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT
dispatch_async(dispatch_get_global_queue(priority, 0), {
PHPhotoLibrary.sharedPhotoLibrary().performChanges({
let createAssetRequest = PHAssetChangeRequest.creationRequestForAssetFromImage(image)
let assetPlaceholder = createAssetRequest.placeholderForCreatedAsset
if let albumChangeRequest = PHAssetCollectionChangeRequest(forAssetCollection: self.assetCollection, assets: self.photosAsset) {
albumChangeRequest.addAssets([assetPlaceholder])
}
}, completionHandler: {(success, error)in
dispatch_async(dispatch_get_main_queue(), {
NSLog("Adding Image to Library -> %#", (success ? "Sucess":"Error!"))
})
})
})
}
You can check this project sample.

Resources