繁体   English   中英

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

[英]How to create and save specific photo album using UIActivityViewController in iOS

我想创建自定义相册并将照片保存到iOS 7中的特定相册,并且我发现iOS使用ALAssetsLibrary 将照片保存在应用程序特定的相册中。

但是我不知道如何使用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];
        }



    }];

我意识到这个问题还有几个月的历史,并且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为“是”的情况下执行此部分。

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? 认为这样! ;-)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM