AVfoundation 将照片保存到自定义相册 iOS 8
AVfoundation save photo to custom album iOS 8
我使用 AVfoundation
框架 (objective 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
中的自定义相册吗?
非常感谢。
以下方法在 ALAssetsLibrary+CustomPhotoAlbum 中不起作用,在 iOS8
、class:
-(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];
}];
}
我测试了一些用于创建相册的旧代码,这些代码仍然有效。我为您制作了一些辅助方法,因此您不必拼凑代码块。希望这有助于了解您所做的不同之处。
#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
我使用 AVfoundation
框架 (objective 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
中的自定义相册吗?
非常感谢。
以下方法在 ALAssetsLibrary+CustomPhotoAlbum 中不起作用,在 iOS8
、class:
-(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];
}];
}
我测试了一些用于创建相册的旧代码,这些代码仍然有效。我为您制作了一些辅助方法,因此您不必拼凑代码块。希望这有助于了解您所做的不同之处。
#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