将图片保存到自定义相册中

8
我一直在按照这个教程http://www.touch-code-magazine.com/ios5-saving-photos-in-custom-photo-album-category-for-download/来将图片保存到自定义相册,虽然功能正常,但它同时将图片保存到了相机胶卷和我的自定义相册中。
根据代码的表现,似乎这是必要的步骤,因为在将图像保存到相机胶卷之后,我们有一个可以与ALAssetsGroup addAsset: 方法一起使用的ALAsset。
有没有办法将图像添加到自定义相册而不将其添加到相机胶卷中呢?
目前正在使用以下代码:
-(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];

                  }];
}

-(void)addAssetURL:(NSURL*)assetURL toAlbum:(NSString*)albumName withCompletionBlock:(SaveImageCompletion)completionBlock
{
__block BOOL albumWasFound = NO;

//search all photo albums in the library
[self enumerateGroupsWithTypes:ALAssetsGroupAlbum 
                    usingBlock:^(ALAssetsGroup *group, BOOL *stop) {

                        //compare the names of the albums
                        if ([albumName compare: [group valueForProperty:ALAssetsGroupPropertyName]]==NSOrderedSame) {

                            //target album is found
                            albumWasFound = YES;

                            //get a hold of the photo's asset instance
                            [self assetForURL: assetURL 
                                  resultBlock:^(ALAsset *asset) {

                                      //add photo to the target album
                                      [group addAsset: asset];

                                      //run the completion block
                                      completionBlock(nil);

                                  } failureBlock: completionBlock];

                            //album was found, bail out of the method
                            return;
                        }

                        if (group==nil && albumWasFound==NO) {
                            //photo albums are over, target album does not exist, thus create it

                            __weak ALAssetsLibrary* weakSelf = self;

                            //create new assets album
                            [self addAssetsGroupAlbumWithName:albumName 
                                                  resultBlock:^(ALAssetsGroup *group) {

                                                      //get the photo's instance
                                                      [weakSelf assetForURL: assetURL 
                                                                    resultBlock:^(ALAsset *asset) {

                                                                        //add photo to the newly created album
                                                                        [group addAsset: asset];

                                                                        //call the completion block
                                                                        completionBlock(nil);

                                                                    } failureBlock: completionBlock];

                                                  } failureBlock: completionBlock];

                            //should be the last iteration anyway, but just in case
                            return;
                        }

                    } failureBlock: completionBlock];

}
1个回答

6
原来并没有这样的选项 - 图片始终会默认写入相机胶卷。

是的,这是真的。所有的图片实际上都在相机胶卷中。相册中的图片只是指向相机胶卷中的图片的指针。 - GeneCode

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接