我创建了一个带AVfoundation
框架的自定义相机(目标C),我正在使用
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
但它只保存到默认相机相册。我想将照片保存在自定义相册中,但ALAssetsLibrary
或ALAssetsLibrary + CustomPhotoAlbum.h不再使用iOS8
。我已尝试过所有内容但iOS8
无效。
所以以下内容不起作用:
[self.library saveImage:image toAlbum:@"CustomAlbum" withCompletionBlock:^(NSError *error) {
if (error!=nil) {
NSLog(@"Big error: %@", [error description]);
}
}];
你能帮我把照片保存到iOS8
的自定义相册吗?
非常感谢你。
以下方法在iOS8
中的ALAssetsLibrary + CustomPhotoAlbum内无法使用,类:
-(void)saveImage:(UIImage*)image toAlbum:(NSString*)albumName withCompletionBlock:(SaveImageCompletion)completionBlock
{
//write the image data to the assets library (camera roll)
[self writeImageToSavedPhotosAlbum:image.CGImage orientation:(ALAssetOrientation)image.imageOrientation
completionBlock:^(NSURL* assetURL, NSError* error) {
//error handling
if (error!=nil) {
completionBlock(error);
return;
}
//add the asset to the custom photo album
[self addAssetURL: assetURL
toAlbum:albumName
withCompletionBlock:completionBlock];
}];
}
答案 0 :(得分:0)
我测试了一些用于创建相册的旧代码,这仍然有用。我为你做了一些帮助方法,所以你不必拼凑代码块。希望这有助于了解您的不同之处。
#import "AppDelegate.h"
#import <AssetsLibrary/AssetsLibrary.h>
@interface AppDelegate()
@end
@implementation AppDelegate
static NSString * const customPhotoAlbumName = @"CustomAlbumv2";
- (void) setupPhotoAlbumNamed: (NSString*) photoAlbumName withCompletionHandler:(void(^)(ALAssetsLibrary*, ALAssetsGroup*))completion
{
ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init];
__weak ALAssetsLibrary *weakAssetsLibrary = assetsLibrary;
[assetsLibrary addAssetsGroupAlbumWithName:photoAlbumName resultBlock:^(ALAssetsGroup *group)
{
NSLog(@"%@ Album result: %@", self, (group.editable ? @"success" : @"already existed"));
if (!group)
{
[weakAssetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAlbum usingBlock:^(ALAssetsGroup *g, BOOL *stop) {
if ([[g valueForProperty:ALAssetsGroupPropertyName] isEqualToString:customPhotoAlbumName])
{
completion(weakAssetsLibrary, g);
}
} failureBlock:^(NSError *error) {
NSLog(@"%@ An error has occured with description: %@", self, error.localizedDescription);
completion(weakAssetsLibrary, nil);
}];
}
else
{
completion(weakAssetsLibrary, group);
}
} failureBlock:^(NSError *error)
{
NSLog(@"%@ An error has occured with description: %@", self, error.localizedDescription);
completion(weakAssetsLibrary, nil);
}];
}
- (void) addImage: (UIImage*) image toAssetsLibrary: (ALAssetsLibrary*) assetsLibrary withGroup: (ALAssetsGroup*) group
{
[assetsLibrary writeImageDataToSavedPhotosAlbum:UIImagePNGRepresentation(image) metadata:nil
completionBlock:
^(NSURL *assetURL, NSError *error)
{
if (error)
{
NSLog(@"%@ An error has occured with description: %@", self, error.localizedDescription);
}
else
{
[assetsLibrary assetForURL:assetURL resultBlock:^(ALAsset *asset)
{
[group addAsset:asset];
NSLog(@"%@ Image was succesfully added!", self);
} failureBlock:^(NSError *error) {
NSLog(@"%@ An error has occured with description: %@", self, error.localizedDescription);
}];
}
}];
}
#pragma mark - UIApplicationDelegate
- (BOOL) application: (UIApplication*) application didFinishLaunchingWithOptions: (NSDictionary*) launchOptions
{
UIImage *imageToAddToAlbum = [UIImage imageNamed:@"test"];
[self setupPhotoAlbumNamed:customPhotoAlbumName withCompletionHandler:
^(ALAssetsLibrary *assetsLibrary, ALAssetsGroup *group) {
if (group)
{
[self addImage:imageToAddToAlbum toAssetsLibrary:assetsLibrary withGroup:group];
}
}];
return YES;
}
@end