Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS save photo in an app specific album

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!

like image 708
Todd Davies Avatar asked Aug 15 '12 15:08

Todd Davies


People also ask

How do I save a photo to a specific album?

Step 1: Press and hold your finger over one photo to begin selecting photos for your album. Step 2: Once all are selected, tap on the more options menu icon (three dots in the top bar), then select Add to Album. Step 3: Tap on the album you would like to add the photos to.

How do I change where my iPhone saves photos?

In your “Settings” menu, find and tap on “Camera.” In the “Camera” menu, tap on “Formats” at the very top of the menu. Here you can select either “High Efficiency,” which will allow your iPhone to shoot and store HEIC files, or “Most Compatible,” which will have your phone capture JPEGs.

How do I select an album for a photo widget?

Currently, there is no way for you to select which albums are available via the Photos Widget. Instead, you rely on the Memories and “Featured Photos” albums that are available in the Photos app. Open the Photos app on your iPhone. Tap the For You tab at the bottom.

How do I add photos to an album on my iPhone?

Tap Albums at the bottom of the screen. Tap , then choose New Album. Name the album, then tap Save. Tap the photos you want to add to the album, then tap Done. To create a shared album, see Share photos with Shared Albums in iCloud. Tap Library at the bottom of the screen, then view your photos by Days or All Photos.

How do I use the recents album on my iPhone?

The Recents album shows your entire collection in the order that you added them to your library. When you use iCloud Photos, the changes that you make to your albums on one device appear on your other devices too. Open Photos. Go to Albums and tap the Add button .

How do I sort photos in an album on Android?

Go to an album, then tap the More button . Tap Sort, then choose a sorting option, like Custom Order, Oldest to Newest, or Newest to Oldest. You can share photos, videos, and albums with select people, then allow them to add their own photos, videos, and comments.

How do I view and organize my photos in an album?

Use the Photos app to view and organize your photos in albums. If you use iCloud Photos, albums are stored in iCloud. They’re up to date and accessible on devices where you’re signed in with the same Apple ID. See Use iCloud Photos on iPhone. Tap Albums at the bottom of the screen. Tap , then choose New Album. Name the album, then tap Save.


2 Answers

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) {

}];
like image 129
kkocabiyik Avatar answered Oct 26 '22 15:10

kkocabiyik


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)
            }
        }
    )
}
like image 32
Eddy Borja Avatar answered Oct 26 '22 15:10

Eddy Borja