Camera App - Create new album with PHPhotoLibrary with .addOnly PHAccessLevel - swift

I have a camera app which takes pictures with iPhone cameras and saves it to the cameraRoll. It requires me to ask for PHPhotoLibrary PHAccessLevel as .addOnly.
An app should save pictures to specific Album - what forces me to check if album exists and to create one if not.
//Create variable that will store Album name
let albumName = "AppName"
var assetCollection: PHAssetCollection!
//Functions that checks if album with specific name exists and saves it to assetCollection variable
if let assetCollection = fetchAssetCollectionForAlbum() {
self.assetCollection = assetCollection
return
}
func fetchAssetCollectionForAlbum() -> PHAssetCollection? {
let fetchOptions = PHFetchOptions()
fetchOptions.predicate = NSPredicate(format: "title = %#", albumName)
let collection = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .any, options: fetchOptions)
if let _: AnyObject = collection.firstObject {
return collection.firstObject
}
return nil
}
//Create album if doesn't exists
func createAlbum() {
if assetCollection == nil {
//If assetCollection is empty (album doesn't exist), create one
PHPhotoLibrary.shared().performChanges({
PHAssetCollectionChangeRequest.creationRequestForAssetCollection(withTitle: albumName) // create an asset collection with the album name
}) { success, error in
if success {
self.assetCollection = self.fetchAssetCollectionForAlbum()
} else {
//With PHAccessLevel as .addOnly error describes that .readWrite access required
print("error \(String(describing: error))")
}
}
}
}
The problem is that above functions requires full access to users photoLibrary. Is there a way to handle album creation without asking for full .readWrite PHAccessLevel?

Related

Images not saving in Custom Folder in Gallery in iOS swift

I am trying to save my images in custom folder in iPhone gallery but instead of saving original image it saves only white blank image on real device. but when I run same code on simulator its working fine.
the code I am trying is :
import Photos
class CustomPhotoAlbum: NSObject {
static let albumName = "App Name" // here put your album name
static let sharedInstance = CustomPhotoAlbum()
var assetCollection: PHAssetCollection!
private var collection: PHFetchResult<PHAssetCollection>!
override init() {
super.init()
if let assetCollection = fetchAssetCollectionForAlbum() {
self.assetCollection = assetCollection
return
}
if PHPhotoLibrary.authorizationStatus() != PHAuthorizationStatus.authorized {
PHPhotoLibrary.requestAuthorization({ (status: PHAuthorizationStatus) -> Void in
()
})
}
if PHPhotoLibrary.authorizationStatus() == PHAuthorizationStatus.authorized {
self.createAlbum()
} else {
PHPhotoLibrary.requestAuthorization(requestAuthorizationHandler)
}
}
func requestAuthorizationHandler(status: PHAuthorizationStatus) {
if PHPhotoLibrary.authorizationStatus() == PHAuthorizationStatus.authorized {
// ideally this ensures the creation of the photo album even if authorization wasn't prompted till after init was done
print("trying again to create the album")
self.createAlbum()
} else {
print("should really prompt the user to let them know it's failed")
}
}
func createAlbum() {
PHPhotoLibrary.shared().performChanges({
PHAssetCollectionChangeRequest.creationRequestForAssetCollection(withTitle: CustomPhotoAlbum.albumName) // create an asset collection with the album name
}) { success, error in
if success {
self.assetCollection = self.fetchAssetCollectionForAlbum()
} else {
print("error \(error)")
}
}
}
func fetchAssetCollectionForAlbum() -> PHAssetCollection? {
var firstObject: PHAssetCollection?
let fetchOptions = PHFetchOptions()
fetchOptions.predicate = NSPredicate(format: "title = %#", CustomPhotoAlbum.albumName)
DispatchQueue.main.async {
self.collection = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .any, options: fetchOptions)
if let _: AnyObject = self.collection.firstObject {
firstObject = self.collection.firstObject
}
}
if firstObject != nil {
return firstObject
} else {
return nil
}
}
func save(image: UIImage) {
if assetCollection == nil {
return // if there was an error upstream, skip the save
}
PHPhotoLibrary.shared().performChanges({
let assetChangeRequest = PHAssetChangeRequest.creationRequestForAsset(from: image)
let assetPlaceHolder = assetChangeRequest.placeholderForCreatedAsset
let albumChangeRequest = PHAssetCollectionChangeRequest(for: self.assetCollection)
let enumeration: NSArray = [assetPlaceHolder!]
albumChangeRequest!.addAssets(enumeration)
}, completionHandler: nil)
}
}
and when I use this code to save the image is:
func saveImage() {
let snapShot:UIView = backgroundImage.snapshotView(afterScreenUpdates: true)!
UIGraphicsBeginImageContext(backgroundImage.bounds.size)
snapShot.drawHierarchy(in: backgroundImage.bounds, afterScreenUpdates: true)
let image:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
CustomPhotoAlbum.sharedInstance.save(image: image)
self.view.snapshotView(afterScreenUpdates: true)
}
Also I add permissions in my info.plist.
but the code is running fine on simulator but not on real device. On real device its also log an error in console:
"Error returned from daemon: Error Domain=com.apple.accounts Code=7
"(null)"" 2022-08-30 17:25:21.043516+0500 App Name[19316:1400881]
[PAAccessLogger] Failed to log access with error: access=<PATCCAccess
0x28247e5b0> accessor:<<PAApplication 0x2809d98b0
identifierType:auditToken identifier:{pid:19316, version:56046}>>
identifier:08A3A297-406E-45A2-8D0D-4C443A3F2835 kind:intervalEnd
timestampAdjustment:0 tccService:kTCCServicePhotos, error=Error
Domain=PAErrorDomain Code=10 "Possibly incomplete access interval
automatically ended by daemon"
I am using Xcode 13.3, Almost same question here asked, I tried every answer but nothing helped.
can someone please help me to fix this issue Thanks.

iOS 14: Create album with new "Selected Photos..." permission? and Fetch AssetCollection For Album?

Step:1 In Gallery Permission i select "Selected Photos...".
Step:2 I create Custom Photo Album.
Step:3 And Try to Fetch AssetCollection from album.
Step:4 It give collection.firstObject = nil.
Step:5 It Work Good if i select Allow Access to All Photos.
For Create album i use :
#objc func createAlbumIfNeeded() {
if let assetCollection = fetchAssetCollectionForAlbum() {
// Album already exists
print("album Already exist")
self.assetCollection = assetCollection
} else {
PHPhotoLibrary.shared().performChanges({
PHAssetCollectionChangeRequest.creationRequestForAssetCollection(withTitle: CustomPhotoAlbum.albumName) // create an assetadsasdasddsa colsadasdasddfsfsdfsfsdfsfsfsdfsdfsdfsdfsdfsddsdf,nbmbmnbmnbmnsdasdasdlection with the album name
}) { success, error in
if success {
self.assetCollection = self.fetchAssetCollectionForAlbum()
print("Album Created")
} else {
print(error?.localizedDescription)
// Unable to create album
}
}
}
}
-> and For Fetch AssetCollection For Album: -
func fetchAssetCollectionForAlbum() -> PHAssetCollection? {
let fetchOptions = PHFetchOptions()
fetchOptions.predicate = NSPredicate(format: "title = %#", CustomPhotoAlbum.albumName)
let collection = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .any, options: fetchOptions)
if let _: AnyObject = collection.firstObject {
return collection.firstObject as! PHAssetCollection
}
return nil
}
Selected Photos Option Which allow Selected Photos option in our application.
-> If you want to create album in Photo-library in iPhone So Allow Access to All Photos option is Compulsory but if you want to get photos from Photo-library Than Selected Photos option will work.

Retrieve firebase Image - ImageView

Hello everyone I am trying to take my image from the firebase database and present it into an image view as the profile image of the user. The first thing I do is create a method to retrieve the photo data in an array
class PhotoService {
static func retrievePhotos(completion: #escaping ([Photo]) -> Void) {
//get a databas refrence
let db = Firestore.firestore()
//get data from "photos" collection
db.collection("photos").getDocuments { (snapshot, Error) in
//check for errors
if Error != nil {
//error in retrieving photos
return
}
//get all the documents
let documents = snapshot?.documents
//check that documents arent nil
if let documents = documents {
//create an array to hold all of our photo structs
var photoArray = [Photo]()
//loop through documents, get a photos struct for each
for doc in documents {
//create photo struct
let p = Photo(snapshot: doc)
if p != nil {
//store it in an array
photoArray.insert(p!, at: 0)
}
}
//pass back the photo array
completion(photoArray)
}
}
}
Then I call that class and attempt to display the Image in the image view
#IBOutlet var profilePictureImageView: UIImageView!
var photos = [Photo]()
override func viewDidLoad() {
super.viewDidLoad()
//call the photo service to retrieve the photos
PhotoService.retrievePhotos { (retrievedPhotos) in
//set the photo array to the retrieved photos
self.photos = retrievedPhotos
//make the image view a circle
self.profilePictureImageView.layer.cornerRadius = self.profilePictureImageView.bounds.height / 2
self.profilePictureImageView.clipsToBounds = true
//make the image view display the photo
var photo:Photo?
func displayPhoto(photo:Photo) {
//check for errors
if photo.photourl == nil {
return
}
//download the image
let photourl = URL(string: photo.photourl!)
//check for erorrs
if photourl == nil {
return
}
//use url session to download the image asynchronously
let session = URLSession.shared
let dataTask = session.dataTask(with: photourl!) { (data, response, Error) in
//check for errors
if Error == nil && data != nil {
//let profilePictureImageView = UIImage()
let image = UIImage(data: data!)
//set the image view
DispatchQueue.main.async {
self.profilePictureImageView.image = image
}
}
}
dataTask.resume()
}
}
}
}
I dont understand what I am doing wrong and why the image is not showing up if anyone can explain this to me and give me an example with code that would be amazing, explain it as though you are explaining it to a kid in grade 5
A quick way to get Image from Firebase and assigning it to an ImageView can be done easily in these Steps.
Function to Get PhotoUrl
//Function to get photo of loggedin User
func getUrl(Completion:#escaping((String)->())) {
let userID = Auth.auth().currentuser?.uid
let db = Firestore.firestore().collection("photos").document(userID)
db.getDocument { (docSnapshot, error) in
if error != nil {
return
} else {
guard let snapshot = docSnapshot, snapshot.exists else {return}
guard let data = snapshot.data() else {return}
let imageUrl = data["photoUrl"] as! String
completion(imageUrl)
}
}
}
To download image and assign it to image view
//Call this function in VC where your `ImageView` is
func getImage(Url:String){
DispatchQueue.global().async {
let url = URL(string: Url)
if let data = try? Data(contentsOf: url!) {
DispatchQueue.main.async {
self.profilePictureImageView.image = UIImage(data: data)
}
}
}
}
}
Call these inside viewDidLoad like this:
getUrl{(url) in
getImage(Url:url)
}

Move PHAsset from one Album to Another (Swift)

I want to move a PHAsset from one Album to another album. Here is what I am doing:
func saveImage(image: UIImage, album: PhotoAlbum, completion: (PHFetchResult?)->()) {
var placeholder: PHObjectPlaceholder?
PHPhotoLibrary.sharedPhotoLibrary().performChanges({
// Request creating an asset from the image
let createAssetRequest = PHAssetChangeRequest.creationRequestForAssetFromImage(image)
// Request editing the album
guard let albumChangeRequest = PHAssetCollectionChangeRequest(forAssetCollection: album) else {
assert(false, "Album change request failed")
return
}
// Get a placeholder for the new asset and add it to the album editing request
guard let photoPlaceholder = createAssetRequest.placeholderForCreatedAsset else {
assert(false, "Placeholder is nil")
return
}
placeholder = photoPlaceholder
albumChangeRequest.addAssets([photoPlaceholder])
}, completionHandler: { success, error in
guard let placeholder = placeholder else {
assert(false, "Placeholder is nil")
completion(nil)
return
}
if success {
completion(PHAsset.fetchAssetsWithLocalIdentifiers([placeholder.localIdentifier], options: nil))
}
else {
print(error)
completion(nil)
}
})
}
The problem is that it creates a copy of it to destination rather than moving. At last, I ended up getting same images in different Albums.

How do I save a movie to a custom named album in swift

in objective C one could save a video to a custom named photo album using code similar to what follows
-(void)saveImage:(UIImage*)image
toAlbum:(NSString*)albumName
withCompletionBlock:(SaveImageCompletion)completionBlock;
I can't figure out how to do this ins swift, when I look at the definition of ALAssetsLibrary I don't see any function call toAlum or savedImage. I can't seem to figure this out?
Here is the code I used
var albumFound : Bool = false
var assetCollection: PHAssetCollection!
var photosAsset: PHFetchResult!
var assetThumbnailSize:CGSize!
// Create the album if does not exist (in viewDidLoad)
if let first_Obj:AnyObject = collection.firstObject{
//found the album
self.albumFound = true
self.assetCollection = collection.firstObject as PHAssetCollection
}else{
//Album placeholder for the asset collection, used to reference collection in completion handler
var albumPlaceholder:PHObjectPlaceholder!
//create the folder
NSLog("\nFolder \"%#\" does not exist\nCreating now...", albumName)
PHPhotoLibrary.sharedPhotoLibrary().performChanges({
let request = PHAssetCollectionChangeRequest.creationRequestForAssetCollectionWithTitle(albumName)
albumPlaceholder = request.placeholderForCreatedAssetCollection
},
completionHandler: {(success:Bool, error:NSError!)in
NSLog("Creation of folder -> %#", (success ? "Success":"Error!"))
self.albumFound = (success ? true:false)
if(success){
let collection = PHAssetCollection.fetchAssetCollectionsWithLocalIdentifiers([albumPlaceholder.localIdentifier], options: nil)
self.assetCollection = collection?.firstObject as PHAssetCollection
}
})
}
let bundle = NSBundle.mainBundle()
let myFilePath = bundle.pathForResource("highlight1", ofType: "mov")
let videoURL:NSURL = NSURL.fileURLWithPath(myFilePath!)!
let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT
dispatch_async(dispatch_get_global_queue(priority, 0), {
PHPhotoLibrary.sharedPhotoLibrary().performChanges({
//let createAssetRequest = PHAssetChangeRequest.creationRequestForAssetFromImage
let createAssetRequest = PHAssetChangeRequest.creationRequestForAssetFromVideoAtFileURL(videoURL)
let assetPlaceholder = createAssetRequest.placeholderForCreatedAsset
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!"))
//picker.dismissViewControllerAnimated(true, completion: nil)
})
})
})
keep in mind what I'm doing is copying a file from my bundle into this album (pre populateing it with videos)