如何使用iOS中的UIActivityViewController创建和保存特定相册

时间:2013-12-31 01:48:34

标签: ios uiactivityviewcontroller

我想创建自定义相册并将照片保存到iOS 7中的特定相册。我使用ALAssetsLibrary找到了iOS save photo in an app specific album

但我不知道如何使用UIActivityViewController进行操作。

NSArray* actItems = [NSArray arrayWithObjects: image, nil];

    UIActivityViewController *activityView = [[UIActivityViewController alloc]
                                              initWithActivityItems:actItems
                                              applicationActivities:nil];


    [activityView setCompletionHandler:^(NSString *activityType, BOOL completed)
    {

        if ([activityType isEqualToString:UIActivityTypeSaveToCameraRoll])
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"#Saved_title", nil)
                                                            message:NSLocalizedString(@"#Saved_message2", nil)
                                                           delegate:self
                                                  cancelButtonTitle:NSLocalizedString(@"OK", nil)
                                                  otherButtonTitles: nil];
            [alert show];
        }



    }];

1 个答案:

答案 0 :(得分:4)

我意识到这个问题已经有几个月了,OP可能已经开始了,但我需要做到这一点,但是没有找到另一个解决方案,需要提出我自己的解决方案。

这不是万无一失的,就像你想要复制的相机胶卷中有多个视频/图像副本一样,它会复制所有这些副本,这可能是也可能不是。此外,为了我的目的,我正在考虑维护一个我写入自定义相册的资产URL的持久列表,以便我将来跳过该资产,但我还没有对它进行编码,因为如果用户删除来自自定义相册的视频/图像,如果没有更多的资产组迭代,几乎不可能更新该列表:我想避免的事情。

最后的免责声明:我还没有测试过整整一吨,但它适用于我的目的,所以希望其他人可以从中受益。

我增加了Marin Todorov的ALAssetsLibrary + CustomPhotoAlbum类别,我在这里找到:https://github.com/yusenhan/Smooth-Line-View/tree/master/ALAssetsLibrary%2BCustomPhotoAlbum。注意:那里没有提供许可证信息,所以我假设修改此类别的来源是可以的。

这是我设置和显示UIActivityViewController的方法:

- (void)showActivitySheet:(NSString *)path {
    NSURL *newURL = [NSURL fileURLWithPath:path];
    NSArray *itemsToShare = @[newURL];
    UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:itemsToShare applicationActivities:nil];
    activityVC.excludedActivityTypes = @[UIActivityTypePrint, UIActivityTypeAssignToContact, UIActivityTypeAddToReadingList];

    // Once the OS has finished with whatever the user wants to do with it,
    // we'll begin figuring out if we need to save it to the Album, too!
    [activityVC setCompletionHandler:^(NSString *activityType, BOOL completed) {

        // If the user selected "Save to Camera Roll"
        if ([activityType isEqualToString:UIActivityTypeSaveToCameraRoll]) {

            ALAssetsLibrary *lib = [[ALAssetsLibrary alloc] init];
            NSURL *tempUrl = [NSURL fileURLWithPath:path];

            // saveMovie: below is a method I added to the category, but you can use saveImage:,
            // which you'll also likely want to add some kind of parameter to so, in the category,
            // you know when you only want to copy to the Album instead of the Album AND Camera Roll
            [lib saveMovie:tempUrl toAlbum:@"CUSTOM ALBUM NAME"
                       withCompletionBlock:^(NSURL *url, NSError *error) {
                           if (error) {
                               NSLog(@"Error writing movie to custom album: %@", error.debugDescription);
                           }
                       } 
   onlyWriteToCustomAlbum:YES];
        }
    }];

    [self presentViewController:activityVC animated:YES completion:nil];
}

以下是您可以添加到ALAssetsLibrary + CustomPhotoAlbum类别中的saveImage:方法的代码,因此您也可以将其保存到自定义相册中!您只能在BOOL的YES情况下执行此部分,您将要添加到该类别。

NSData *dataToCompareTo = [NSData dataWithContentsOfURL:url]; // This data represents the image/movie which you want to save into the custom album. The one you handed to the UIActivityViewController.

    // Note use of self, as this is a category on ALAssetsLibrary; also, this
    // assumes that you've already written the photo to the Camera Roll, which
    // should be automatically handled by the OS, if the user hit the "Save" button
    // on the UIActivityViewController.

    // Enumerate through Camera Roll/Saved Photos group.
    [self enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos
                       usingBlock:^(ALAssetsGroup *group, BOOL *stop) {

                           // Enumerate the assets in each group. 
                           [group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {

                               ALAssetRepresentation *rep = [result defaultRepresentation];

                               // If the asset isn't the same size as the source image/movie
                               // it shouldn't be the same. Check the size first, since it
                               // is a less costly operation than byte checking the data itself.
                               if ([rep size] == [dataToCompareTo length]) {
                                   Byte *buffer = malloc([NSNumber numberWithLongLong:rep.size].unsignedLongValue);
                                   NSUInteger buffered = [rep getBytes:buffer fromOffset:0.0 length:[NSNumber numberWithLongLong:rep.size].unsignedLongValue error:nil];
                                   NSData *data = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES];

                                   // If the buffer has more than the null-termination char, free it!  I'm doing this in case the above call to dataWithBytesNoCopy fails to free() the buffer.
                                   if (sizeof(buffer) > 4) {
                                       free(buffer);
                                   }

                                   // Ensure they are the same by comparing the NSData instances.
                                   if ([data isEqualToData:dataToCompareTo]) {
                                       NSLog(@"they are the same!!");
                                       [self addAssetURL:[rep url]
                                                 toAlbum:albumName
                                withMovieCompletionBlock:completionBlock];
                                   } else {
                                       NSLog(@"they are diffrnt");
                                   }
                               }
                           }];
                       } failureBlock:^(NSError *error) {
                           NSLog(@"Failed to write to custom album!");
                       }];

希望它有所帮助! TLDR?认为这样! ; - )

相关问题