web-dev-qa-db-ja.com

iOSはアプリ固有のアルバムに写真を保存します

IOS5アプリを作成しています。写真をデバイスに保存したい。

アプリ固有のアルバムに写真を保存したいので、アルバムを作成してからアルバムに保存する必要があります。

私はアルバムの作成方法を知っています:

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
}];

今すぐ新しいアルバムに写真を追加したいのですが、どうすればよいですか?サンプルコードは大歓迎です!

14
Todd Davies

次のコードを使用して、アルバムの名前を変更することができます。

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

}];
21
kkocabiyik

IOS 9の時点でこれを実行しようとしている人にとっては、ALAssetsLibraryが非推奨になり、新しいPhotosライブラリが採用されたため、状況は少し複雑になっています。

ここにいくつかのSwift特定のアルバム名にUIImagesを追加するためのコード(アルバムが存在しない場合はアルバムを作成する)があります。ニーズに合わせてリファクタリング/最適化を行う必要がある場合があります。

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)
            }
        }
    )
}
11
Eddy Borja

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);
        }
    }];
}
10
Xeaza

Swift 4:に対するEddyの答えの適応

    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
            }
        })
    }
1
Pacificana

ブロックを使用したObjectiveCの改良版。アルバムが存在しない場合は作成し、写真、GIF、ビデオの3種類のメディアアイテムを保存します。

// 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 : @"");
            }

        }];

    }];
}

これらの方法を使用して、メディアアイテムを次のように保存できます。

[self saveToAlbumMediaItemFromURL:[self.attachmentStream mediaURL] mediaType:AlbumMediaTypeGIF];
0
Denis Kutlubaev