iOS save photo in an app specific album

iphone move photos to album delete from camera roll
how to move a photo to an album on iphone
how to change photo album cover on iphone
rearrange photos in iphone album
iphone photo album sort by date
how do i move a photo from camera roll to an album?
how to separate camera photos in iphone
how to organize photos on iphone by date

I'm creating an iOS 5 app. I want to save a photo to the device.

I want to save the photo to an album specific to my app, so I need to create the album, and then save photos to the album.

I know how to create the album:

ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library addAssetsGroupAlbumWithName:@"MY APP NAME" resultBlock:^(ALAssetsGroup *group) {
    //How to get the album URL?
} failureBlock:^(NSError *error) {
    //Handle the error
}];

I want add photos to the new album now, how do I do so? Sample code is greatly appreciated!

You may use the following code just change the name of album :

__weak ALAssetsLibrary *lib = self.library;

[self.library addAssetsGroupAlbumWithName:@"My Photo Album" resultBlock:^(ALAssetsGroup *group) {

    ///checks if group previously created
    if(group == nil){

        //enumerate albums
        [lib enumerateGroupsWithTypes:ALAssetsGroupAlbum
                           usingBlock:^(ALAssetsGroup *g, BOOL *stop)
         {
             //if the album is equal to our album
             if ([[g valueForProperty:ALAssetsGroupPropertyName] isEqualToString:@"My Photo Album"]) {

                 //save image
                 [lib writeImageDataToSavedPhotosAlbum:UIImagePNGRepresentation(image) metadata:nil
                                       completionBlock:^(NSURL *assetURL, NSError *error) {

                                           //then get the image asseturl
                                           [lib assetForURL:assetURL
                                                resultBlock:^(ALAsset *asset) {
                                                    //put it into our album
                                                    [g addAsset:asset];
                                                } failureBlock:^(NSError *error) {

                                                }];
                                       }];

             }
         }failureBlock:^(NSError *error){

         }];

    }else{
        // save image directly to library
        [lib writeImageDataToSavedPhotosAlbum:UIImagePNGRepresentation(image) metadata:nil
                              completionBlock:^(NSURL *assetURL, NSError *error) {

                                  [lib assetForURL:assetURL
                                       resultBlock:^(ALAsset *asset) {

                                           [group addAsset:asset];

                                       } failureBlock:^(NSError *error) {

                                       }];
                              }];
    }

} failureBlock:^(NSError *error) {

}];

Organize photos in albums on iPhone, Use the Photos app to organize your pictures with albums. You can create new albums, or add photos to existing albums. If you use iCloud Photos, albums are  Add photos and videos to existing albums. Tap the Photos tab at the bottom of the screen, then tap Select. Tap the photo and video thumbnails that you want to add, then tap . Swipe up, then tap Add to Album from the list of actions. Tap the album you want to add to.

For anyone looking to do this as of iOS 9, things have gotten a bit more complicated since the ALAssetsLibrary is deprecated in favor of the new Photos library.

Here's some Swift code for adding UIImages to a specific album name (creating the album if it doesn't exist), you may need to do some refactoring/optimization for your needs:

func insertImage(image : UIImage, intoAlbumNamed albumName : String) {

    //Fetch a collection in the photos library that has the title "albumNmame"
    let collection = fetchAssetCollectionWithAlbumName(albumName)

    if collection == nil {
        //If we were unable to find a collection named "albumName" we'll create it before inserting the image
        PHPhotoLibrary.sharedPhotoLibrary().performChanges({
            PHAssetCollectionChangeRequest.creationRequestForAssetCollectionWithTitle(albumName)
            }, completionHandler: {(success : Bool, error : NSError?) in
                if error != nil {
                    print("Error: " + error!.description)
                }

                if success {
                    //Fetch the newly created collection (which we *assume* exists here)
                    let newCollection = self.fetchAssetCollectionWithAlbumName(albumName)
                    self.insertImage(image, intoAssetCollection: newCollection!)
                }
            }
        )
    } else {
        //If we found the existing AssetCollection with the title "albumName", insert into it
        self.insertImage(image, intoAssetCollection: collection!)
    }
}

func fetchAssetCollectionWithAlbumName(albumName : String) -> PHAssetCollection? {

    //Provide the predicate to match the title of the album.
    let fetchOption = PHFetchOptions()
    fetchOption.predicate = NSPredicate(format: "title == '" + albumName + "'")

    //Fetch the album using the fetch option
    let fetchResult = PHAssetCollection.fetchAssetCollectionsWithType(
        PHAssetCollectionType.Album,
        subtype: PHAssetCollectionSubtype.AlbumRegular,
        options: fetchOption)

    //Assuming the album exists and no album shares it's name, it should be the only result fetched
    let collection = fetchResult.firstObject as? PHAssetCollection

    return collection
}

func insertImage(image : UIImage, intoAssetCollection collection : PHAssetCollection) {

    //Changes for the Photos Library must be maded within the performChanges block
    PHPhotoLibrary.sharedPhotoLibrary().performChanges({

            //This will request a PHAsset be created for the UIImage
            let creationRequest = PHAssetCreationRequest.creationRequestForAssetFromImage(image)

            //Create a change request to insert the new PHAsset in the collection
            let request = PHAssetCollectionChangeRequest(forAssetCollection: collection)

            //Add the PHAsset placeholder into the creation request.
            //The placeholder is used because the actual PHAsset hasn't been created yet
            if request != nil && creationRequest.placeholderForCreatedAsset != nil {
                request!.addAssets([creationRequest.placeholderForCreatedAsset!])
            }

        },

        completionHandler: { (success : Bool, error : NSError?) in
            if error != nil {
                print("Error: " + error!.description)
            }
        }
    )
}

How To Use iPhone Photo Albums To Organize Photos, An iPhone photo album is a storage place for a particular set of images within the Photos app. When you To find your iPhone photo albums, open the Photos app​. Then tap Then tap Save to add the photos to the album. On your iPhone, iPad, or iPod touch: Go to Albums and tap the Add button . Enter an album name, then choose the photos and videos that you want to organize in the new album. On your Mac: Choose File > New Album. Enter an album name, then click Photos in the sidebar. Drag photos and videos from the Photos view into the new album in the sidebar.

For those looking for Eddy's answer in Objective-C.

#import <Photos/Photos.h>

- (void)insertImage:(UIImage *)image intoAlbumNamed:(NSString *)albumName {
    //Fetch a collection in the photos library that has the title "albumNmame"
    PHAssetCollection *collection = [self fetchAssetCollectionWithAlbumName: albumName];

    if (collection == nil) {
        //If we were unable to find a collection named "albumName" we'll create it before inserting the image
        [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
            [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle: albumName];
        } completionHandler:^(BOOL success, NSError * _Nullable error) {
            if (error != nil) {
                NSLog(@"Error inserting image into album: %@", error.localizedDescription);
            }

            if (success) {
                //Fetch the newly created collection (which we *assume* exists here)
                PHAssetCollection *newCollection = [self fetchAssetCollectionWithAlbumName:albumName];
                [self insertImage:image intoAssetCollection: newCollection];
            }
        }];
    } else {
        //If we found the existing AssetCollection with the title "albumName", insert into it
        [self insertImage:image intoAssetCollection: collection];
    }
}

- (PHAssetCollection *)fetchAssetCollectionWithAlbumName:(NSString *)albumName {
    PHFetchOptions *fetchOptions = [PHFetchOptions new];
    //Provide the predicate to match the title of the album.
    fetchOptions.predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"title == '%@'", albumName]];

    //Fetch the album using the fetch option
    PHFetchResult *fetchResult = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:fetchOptions];

    //Assuming the album exists and no album shares it's name, it should be the only result fetched
    return fetchResult.firstObject;
}

- (void)insertImage:(UIImage *)image intoAssetCollection:(PHAssetCollection *)collection {
    [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{

        //This will request a PHAsset be created for the UIImage
        PHAssetCreationRequest *creationRequest = [PHAssetCreationRequest creationRequestForAssetFromImage:image];

        //Create a change request to insert the new PHAsset in the collection
        PHAssetCollectionChangeRequest *request = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:collection];

        //Add the PHAsset placeholder into the creation request.
        //The placeholder is used because the actual PHAsset hasn't been created yet
        if (request != nil && creationRequest.placeholderForCreatedAsset != nil) {
            [request addAssets: @[creationRequest.placeholderForCreatedAsset]];
        }
    } completionHandler:^(BOOL success, NSError * _Nullable error) {
        if (error != nil) {
            NSLog(@"Error inserting image into asset collection: %@", error.localizedDescription);
        }
    }];
}

How to Add Several Photos to an Album in the Photos App in iOS 13 , Alternately, tap the New Album option above them, give your new album a name, then tap Save. photos. Frankly, it's probably time Apple  Go to "Photo's" app, click the + (plus sign) in the top left corner! It should say "New Album" and then you enter a name for the album. Go into your regular camera roll and click "edit" in the top right corner and check mark the photos you want to move to the other folder, then press "add to existing" and it should be there!

Adaptation of Eddy's answer for Swift 4:

    func saveImageToAlbum(_ image: UIImage, name: String) {

        if let collection = fetchAssetCollection(name) {
            self.saveImageToAssetCollection(image, collection: collection)
        } else {
            // Album does not exist, create it and attempt to save the image
            PHPhotoLibrary.shared().performChanges({
                PHAssetCollectionChangeRequest.creationRequestForAssetCollection(withTitle: name)
            }, completionHandler: { (success: Bool, error: Error?) in
                guard success == true && error == nil else {
                    NSLog("Could not create the album")
                    if let err = error {
                        NSLog("Error: \(err)")
                    }
                    return
                }

                if let newCollection = self.fetchAssetCollection(name) {
                    self.saveImageToAssetCollection(image, collection: newCollection)
                }
            })
        }
    }

    func fetchAssetCollection(_ name: String) -> PHAssetCollection? {

        let fetchOption = PHFetchOptions()
        fetchOption.predicate = NSPredicate(format: "title == '" + name + "'")

        let fetchResult = PHAssetCollection.fetchAssetCollections(
            with: PHAssetCollectionType.album,
            subtype: PHAssetCollectionSubtype.albumRegular,
            options: fetchOption)

        return fetchResult.firstObject
    }

    func saveImageToAssetCollection(_ image: UIImage, collection: PHAssetCollection) {

        PHPhotoLibrary.shared().performChanges({

            let creationRequest = PHAssetCreationRequest.creationRequestForAsset(from: image)
            if let request = PHAssetCollectionChangeRequest(for: collection),
                let placeHolder = creationRequest.placeholderForCreatedAsset {
                request.addAssets([placeHolder] as NSFastEnumeration)
            }
        }, completionHandler: { (success: Bool, error: Error?) in
            guard success == true && error == nil else {
                NSLog("Could not save the image")
                if let err = error {
                    NSLog("Error: " + err.localizedDescription)
                }
                return
            }
        })
    }

How to Create Photo Albums on Your iPhone, You can create albums on your iPhone to make finding and viewing photos easier. Type in a name for your new Album. Tap Save. The Add Photos screen opens. choose all the photos it contains or tap individual thumbnails to select them. Launch the Photos app from your home screen. Tap an album to view its contents. Tap Select at the top right. Tap on all the photos or videos you'd like to add.

Improved version on Objective C, using blocks. It creates an album, if it doesn't exist, then saves three types of media items - photos, gifs and videos:

// Types of media, that can be saved to an album
typedef NS_ENUM(NSUInteger, AlbumMediaType) {
    AlbumMediaTypePhoto,
    AlbumMediaTypeGIF,
    AlbumMediaTypeVideo
};

/**
 Creates album if it doesn't exist and returns it in a block
 */
- (void)createCollectionOnComplete:(void (^ _Nonnull)(PHAssetCollection * _Nonnull collection))onComplete
{
    NSString *albumTitle = @"YOUR_ALBUM_TITLE";

    __block PHAssetCollection *collection;
    __block PHObjectPlaceholder *placeholder;

    // Searching for an existing album
    PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init];
    fetchOptions.predicate = [NSPredicate predicateWithFormat:@"title = %@", albumTitle];
    collection = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum
                                                          subtype:PHAssetCollectionSubtypeAny
                                                          options:fetchOptions].firstObject;
    // If album is not found, we create it
    if (!collection)
    {
        [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
            PHAssetCollectionChangeRequest *createAlbum = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:albumTitle];
            placeholder = [createAlbum placeholderForCreatedAssetCollection];
        } completionHandler:^(BOOL success, NSError *error) {
            if (success)
            {
                PHFetchResult *collectionFetchResult = [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[placeholder.localIdentifier]
                                                                                                            options:nil];
                collection = collectionFetchResult.firstObject;
                // After creating album, we return it
                onComplete(collection);
            }
        }];
    } else {
        // If album already exists, we instantly return it
        onComplete(collection);
    }
}


/**
 Saves an item of a given mediatype, that is located in mediaURL
 */
- (void)saveToAlbumMediaItemFromURL:(NSURL *)mediaURL mediaType:(AlbumMediaType)mediaType
{
    NSData *mediaData = [NSData dataWithContentsOfURL:mediaURL];
    if (!mediaData) {
        OWSFail(@"%@ Could not load data: %@", self.logTag, [self.attachmentStream mediaURL]);
        return;
    }

    [self createCollectionOnComplete:^(PHAssetCollection * _Nonnull collection) {

        [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{

            // We create a PHAsset using creationRequest
            PHAssetCreationRequest *assetRequest;
            switch (mediaType) {
                case AlbumMediaTypePhoto: {
                    assetRequest = [PHAssetCreationRequest creationRequestForAssetFromImage:[UIImage imageWithData:mediaData]];
                    break;
                }
                case AlbumMediaTypeGIF: {
                    assetRequest = [PHAssetCreationRequest creationRequestForAsset];
                    PHAssetResourceCreationOptions *options = [[PHAssetResourceCreationOptions alloc] init];
                    [assetRequest addResourceWithType:PHAssetResourceTypePhoto data:mediaData options:options];
                    break;
                }
                case AlbumMediaTypeVideo: {
                    if ( !UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(mediaURL.path) ) {
                        OWSFail(@"%@ Could not save incompatible video data.", self.logTag);
                        break;
                    }

                    NSString *videoPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"file.mov"];
                    [mediaData writeToFile:videoPath atomically:YES];
                    assetRequest = [PHAssetCreationRequest creationRequestForAssetFromVideoAtFileURL:[NSURL fileURLWithPath:videoPath]];
                    break;
                }
                default:
                    break;
            }

            // Creating a request to change an album
            PHAssetCollectionChangeRequest *albumChangeRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:collection];

            // PHAsset is not created yet, so we use a placeholder
            PHObjectPlaceholder *placeholder = [assetRequest placeholderForCreatedAsset];

            // We add a placeholder of a created item to the request of changing album
            if (albumChangeRequest != nil && placeholder != nil) {
                [albumChangeRequest addAssets: @[placeholder]];
            }

        } completionHandler:^(BOOL success, NSError *error) {

            if (success) {
                NSLog(@"Media item saved!");
            } else {
                NSLog(@"Error saving media item - %@", error ? error.localizedDescription : @"");
            }

        }];

    }];
}

We can use these methods to save media items this way:

[self saveToAlbumMediaItemFromURL:[self.attachmentStream mediaURL] mediaType:AlbumMediaTypeGIF];

How to organize photos and videos into albums on your iPhone or , How to create new albums with the Photos app for iPhone and iPad; How to create new albums in the Photos app on iOS 13 and iPadOS 13 Tap Save. Is there a particular way you like to keep your images neat and tidy? Installed 13.1.2 and the sign for adding photos to an album has disappeared! i did find, however, that it works in reverse. First click on the album you want to add a photo into. Go to top right and click ‘select’ Then go to the bottom and click ‘add’

How to Move Photos to a Custom Album on an iPad, It's easy enough to create a custom album in the Photos app, but if you If you just see images and no specific album, tap the arrow at the top left of New Album pop-up in Photos on an iPad with the Save button highlighted. Part 5: How to Download Photo Album from iPhone to PC via AutoPlay. This way is also can offer all Windows user, including Windows 7 user and above, to download photo album from iPhone to the computer. Here are the steps below to transfer photo albums from iPhone to PC. Step 1: Connect iPhone to the Computer with USB Cable.

Create & edit photo albums - iPhone & iPad, Create a new album. On your iPhone or iPad, open the Google Photos app Photos . Sign in to your Google Account. Touch and hold a photo, and then select the  I'm new to iPhone (long time Android user but got an iPhone as business phone), so take it with grain of salt. I don't think you can do that. All other photo sync apps like Dropbox or OneDrive or iCloud Photos can't do that.

Download photos or videos to your device - iPhone & iPad, Save a photo or video. On your iPhone or iPad, open the Google Photos app Photos . Select a photo or video. Tap More 

Comments
  • This article seems to achieve what you're looking for with a custom ALAssetsLibrary category. Looks like there are some useful comments below the article that may assist with performance also.
  • Great, Its working. I have removed __weak while declaring ALAssetsLibrary *lib because completionBlock was not calling.
  • Is it possible to add only to that album and not to Camera Roll as well?