Add Assets to an Icloud Shared Photo Album Programmatically

Add assets to an iCloud shared photo album programmatically

Yes, you can't add images to an iCloud shared album.

Prior to performing a change request you should call canPerform(_:) on the PHAssetCollection.

If you call canPerform(_:) on a PHAssetCollectionSubtype of albumCloudShared it will return false.

For example:

    print(collection.canPerform(.createContent)) //false

print(collection.canPerform(.addContent)) //false

I had this same problem and submitted a feature request via the Feedback app.

How to attach a short data to a photo in iOS/macOS cloud shared album?

Following a description of a private API which I found in this answer I created the following extension to PHAsset:

extension PHAsset {
var cloudIdentifier: String {
return (self.value(forKey: "cloudIdentifier") as? String) ?? "?"
}
}

The cloudIdentifier which you can get now from a PHAsset instance is persistent throughout the cloud on any devices sharing the same album. With persistent PHAsset identifier, one can implement solution #3 from the initial question.

iCloud sync of images in Photos Framework created album

After testing it seems that any PHAssets created will sync over iCloud. (I've only tested this with iCloud Photo Library turned on so not 100% sure otherwise).

It also seems that the localIdentifier of PHAssets are exactly what they say: local, and won't persist across devices.

iPhone - How to create a custom album and give custom names to photos in camera roll programmatically?

You can create a custom album and add an image pretty easy with these lines of code in iOS:

// Create the new album.
__block PHObjectPlaceholder *myAlbum;
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
PHAssetCollectionChangeRequest *changeRequest = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:title];
myAlbum = changeRequest.placeholderForCreatedAssetCollection;
} completionHandler:^(BOOL success, NSError *error) {
if (success) {
PHFetchResult *fetchResult = [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[myAlbum.localIdentifier] options:nil];
PHAssetCollection *assetCollection = fetchResult.firstObject;

[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
PHAssetChangeRequest *assetChangeRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:image];

// add asset
PHAssetCollectionChangeRequest *assetCollectionChangeRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:assetCollection];
[assetCollectionChangeRequest addAssets:@[[assetChangeRequest placeholderForCreatedAsset]]];
} completionHandler:^(BOOL success, NSError *error) {
if (!success) {
NSLog(@"Error: %@", error);
}
}];
} else {
NSLog(@"Error: %@", error);
}
}];

Swift - how to get last taken 3 photos from photo library?

Here's a solution using the Photos framework available for devices iOS 8+ :

import Photos

class ViewController: UIViewController {

var images:[UIImage] = []

func fetchPhotos () {
// Sort the images by descending creation date and fetch the first 3
let fetchOptions = PHFetchOptions()
fetchOptions.sortDescriptors = [NSSortDescriptor(key:"creationDate", ascending: false)]
fetchOptions.fetchLimit = 3

// Fetch the image assets
let fetchResult: PHFetchResult = PHAsset.fetchAssets(with: PHAssetMediaType.image, options: fetchOptions)

// If the fetch result isn't empty,
// proceed with the image request
if fetchResult.count > 0 {
let totalImageCountNeeded = 3 // <-- The number of images to fetch
fetchPhotoAtIndex(0, totalImageCountNeeded, fetchResult)
}
}

// Repeatedly call the following method while incrementing
// the index until all the photos are fetched
func fetchPhotoAtIndex(_ index:Int, _ totalImageCountNeeded: Int, _ fetchResult: PHFetchResult<PHAsset>) {

// Note that if the request is not set to synchronous
// the requestImageForAsset will return both the image
// and thumbnail; by setting synchronous to true it
// will return just the thumbnail
let requestOptions = PHImageRequestOptions()
requestOptions.isSynchronous = true

// Perform the image request
PHImageManager.default().requestImage(for: fetchResult.object(at: index) as PHAsset, targetSize: view.frame.size, contentMode: PHImageContentMode.aspectFill, options: requestOptions, resultHandler: { (image, _) in
if let image = image {
// Add the returned image to your array
self.images += [image]
}
// If you haven't already reached the first
// index of the fetch result and if you haven't
// already stored all of the images you need,
// perform the fetch request again with an
// incremented index
if index + 1 < fetchResult.count && self.images.count < totalImageCountNeeded {
self.fetchPhotoAtIndex(index + 1, totalImageCountNeeded, fetchResult)
} else {
// Else you have completed creating your array
print("Completed array: \(self.images)")
}
})
}
}

Delete an asset (picture or video) from IPhone in IOS

Possible duplication of https://stackoverflow.com/a/11058934/300292. Simple answer: you can't. The Photos app is the only place you can delete assets. Which is probably a good thing--you wouldn't want any willy-nilly app to be able to delete all your photos, would you?



Related Topics



Leave a reply



Submit