Instagram のようなギャラリー ビューが必要な iOS アプリに取り組んでいます。ギャラリー ビュー、カメラ ビュー、ビデオ ビューを追加しました。画像を撮影した後、写真のカスタム アルバムに保存します。カスタム アルバムからこれらの画像を取得し、ギャラリーのコレクション ビューに表示します。しかし、カスタムアルバムからそれらの画像を取得するのに行き詰まっています。どんな助けでも大歓迎です。
編集: カスタム フォト アルバムを作成するために PHPhotoLibrary を追加しました。この前に、AssetsLibrary フレームワークを追加しました。
次に、PHPhotoLibrary を使用してカスタム フォト アルバムを作成および管理するための NSObject クラス (CustomAlbum) を作成しました。
// CustomAlbum.h
#import <Foundation/Foundation.h>
#import <Photos/Photos.h>
@interface CustomAlbum : NSObject
//Creating album with given name
+(void)makeAlbumWithTitle:(NSString *)title onSuccess:(void(^)(NSString *AlbumId))onSuccess onError: (void(^)(NSError * error)) onError;
//Get the album by name
+(PHAssetCollection *)getMyAlbumWithName:(NSString*)AlbumName;
//Add a image
+(void)addNewAssetWithImage:(UIImage *)image toAlbum:(PHAssetCollection *)album onSuccess:(void(^)(NSString *ImageId))onSuccess onError: (void(^)(NSError * error)) onError;
//get the image using identifier
+ (void)getImageWithIdentifier:(NSString*)imageId onSuccess:(void(^)(UIImage *image))onSuccess onError: (void(^)(NSError * error)) onError;
@end
// CustomAlbum.m
#import "CustomAlbum.h"
@implementation CustomAlbum
#pragma mark - PHPhoto
+(void)makeAlbumWithTitle:(NSString *)title onSuccess:(void(^)(NSString *AlbumId))onSuccess onError: (void(^)(NSError * error)) onError
{
//Check weather the album already exist or not
if (![self getMyAlbumWithName:title]) {
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
// Request editing the album.
PHAssetCollectionChangeRequest *createAlbumRequest = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:title];
// Get a placeholder for the new asset and add it to the album editing request.
PHObjectPlaceholder * placeHolder = [createAlbumRequest placeholderForCreatedAssetCollection];
if (placeHolder) {
onSuccess(placeHolder.localIdentifier);
}
} completionHandler:^(BOOL success, NSError *error) {
NSLog(@"Finished adding asset. %@", (success ? @"Success" : error));
if (error) {
onError(error);
}
}];
}
}
+(void)addNewAssetWithImage:(UIImage *)image toAlbum:(PHAssetCollection *)album onSuccess:(void(^)(NSString *ImageId))onSuccess onError: (void(^)(NSError * error)) onError
{
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
// Request creating an asset from the image.
PHAssetChangeRequest *createAssetRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:image];
// Request editing the album.
PHAssetCollectionChangeRequest *albumChangeRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:album];
// Get a placeholder for the new asset and add it to the album editing request.
PHObjectPlaceholder * placeHolder = [createAssetRequest placeholderForCreatedAsset];
[albumChangeRequest addAssets:@[ placeHolder ]];
NSLog(@"%@",placeHolder.localIdentifier);
if (placeHolder) {
onSuccess(placeHolder.localIdentifier);
}
} completionHandler:^(BOOL success, NSError *error) {
NSLog(@"Finished adding asset. %@", (success ? @"Success" : error));
if (error) {
onError(error);
}
}];
}
+(PHAssetCollection *)getMyAlbumWithName:(NSString*)AlbumName
{
#if 0
NSString * identifier = [[NSUserDefaults standardUserDefaults]objectForKey:kAlbumIdentifier];
if (!identifier) return nil;
PHFetchResult *assetCollections = [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[identifier]
options:nil];
#else
PHFetchResult *assetCollections = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum
subtype:PHAssetCollectionSubtypeAlbumRegular
options:nil];
#endif
NSLog(@"assetCollections.count = %lu", assetCollections.count);
if (assetCollections.count == 0) return nil;
__block PHAssetCollection * myAlbum;
[assetCollections enumerateObjectsUsingBlock:^(PHAssetCollection *album, NSUInteger idx, BOOL *stop) {
NSLog(@"album:%@", album);
NSLog(@"album.localizedTitle:%@", album.localizedTitle);
if ([album.localizedTitle isEqualToString:AlbumName]) {
myAlbum = album;
*stop = YES;
}
}];
if (!myAlbum) return nil;
return myAlbum;
}
+(NSArray *)getAssets:(PHFetchResult *)fetch
{
__block NSMutableArray * assetArray = NSMutableArray.new;
[fetch enumerateObjectsUsingBlock:^(PHAsset *asset, NSUInteger idx, BOOL *stop) {
NSLog(@"asset:%@", asset);
[assetArray addObject:asset];
}];
return assetArray;
}
+ (void)getImageWithIdentifier:(NSString*)imageId onSuccess:(void(^)(UIImage *image))onSuccess onError: (void(^)(NSError * error)) onError
{
NSError *error = [[NSError alloc] init];
PHFetchResult *assets = [PHAsset fetchAssetsWithLocalIdentifiers:@[imageId] options:nil];
if (assets.count == 0) onError(error);
NSArray * assetArray = [self getAssets:assets];
PHImageManager *manager = [PHImageManager defaultManager];
CGRect screenRect = [[UIScreen mainScreen] bounds];
[manager requestImageForAsset:assetArray.firstObject targetSize:screenRect.size contentMode:PHImageContentModeAspectFit options:nil resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
onSuccess(result);
}];
}
@end
次に、このメソッドを使用して画像ボタンをクリックしてカスタム アルバムを作成し、そのカスタム アルバムに画像を保存します。
// Take Image Button Method
- (void)snapButtonPressed:(UIButton *)button
{
[self.camera capture:^(LLSimpleCamera *camera, UIImage *image,
NSDictionary *metadata, NSError *error)
{
if(!error)
{
NSString * info = [NSString stringWithFormat:@"Size: %@ - Orientation: %ld", NSStringFromCGSize(image.size), (long)image.imageOrientation];
[CustomAlbum addNewAssetWithImage:image toAlbum:[CustomAlbum getMyAlbumWithName:CSAlbum] onSuccess:^(NSString *ImageId) {
NSLog(@"%@",ImageId);
recentImg = ImageId;
} onError:^(NSError *error) {
NSLog(@"probelm in saving image");
}];
}
else
{
NSLog(@"An error has occured: %@", error);
}
}
exactSeenImage:YES];
}