Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/119.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
AVfoundation将照片保存到自定义相册iOS 8_Ios_Xcode_Ios8_Avfoundation - Fatal编程技术网

AVfoundation将照片保存到自定义相册iOS 8

AVfoundation将照片保存到自定义相册iOS 8,ios,xcode,ios8,avfoundation,Ios,Xcode,Ios8,Avfoundation,我已经用AVfoundation framework objective C创建了一个自定义相机,我正在使用 UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil); 但它只是保存到默认的相机相册。我想将照片保存在自定义相册中,但AlasSetLibrary或AlasSetLibrary+CustomPhotoAlbum.h不再使用iOS8。我尝试了所有的方法,但在iOS8中没有任何效果 因此,以下方法不起作用: [self.library s

我已经用AVfoundation framework objective C创建了一个自定义相机,我正在使用

UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
但它只是保存到默认的相机相册。我想将照片保存在自定义相册中,但AlasSetLibrary或AlasSetLibrary+CustomPhotoAlbum.h不再使用iOS8。我尝试了所有的方法,但在iOS8中没有任何效果

因此,以下方法不起作用:

[self.library saveImage:image toAlbum:@"CustomAlbum" withCompletionBlock:^(NSError *error) {
        if (error!=nil) {
            NSLog(@"Big error: %@", [error description]);
        }
    }];
您能帮我将照片保存到iOS8中的自定义相册吗

多谢各位

以下方法在iOS8类中的AlassetLibrary+CustomPhotoAlbum中不起作用:


我测试了一些用于创建相册的旧代码,这些代码仍然有效。我为您制作了一些助手方法,这样您就不必拼凑代码块。希望这有助于了解您的不同做法

#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
#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