OStack程序员社区-中国程序员成长平台

标题: ios - 将照片保存到iOS相册,不要全部保存 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 23:56
标题: ios - 将照片保存到iOS相册,不要全部保存

我正在尝试从应用中保存照片,创建相册并将照片保存到其中,但无法正常工作。

有时它会保存一张或另一张照片,但不会保存所有照片。

向大家致以最诚挚的问候和感谢!

for(PFObject *pf in imageFilesArray)
{
    PFObject *imageObject = pf;
    PFFile *imageFile = [imageObject objectForKey"image"];

    [imageFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
        if (!error) {
            UIImage *image = [UIImage imageWithData:data];

            [self.library writeImageToSavedPhotosAlbum:[image CGImage] orientation:ALAssetOrientationUp completionBlock:^(NSURL *assetURL, NSError *error) {
                if(assetURL) {
                    [self.library assetForURL:assetURL resultBlock:^(ALAsset *asset) {
                        [self addAsset:asset toGroup"App Album" inLib:self.library];
                        NSLog(@"Adicionado");
                    } failureBlock:^(NSError *error) {
                        NSLog(@"%@",error);
                    }];
                }
            }];
        }
    }];
}
[hud hide:NO];

- (void) addAssetALAsset*)a toGroupNSString*)name inLibALAssetsLibrary*)lib 方法

[lib enumerateGroupsWithTypes:ALAssetsGroupAlbum usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
    if([[group valueForProperty:ALAssetsGroupPropertyName] isEqualToString:name]) {
        [group addAsset:a];
        *stop = YES;
        NSLog(@"added asset to EXISTING group");
    }
    if(!group) {
        [lib addAssetsGroupAlbumWithName:name resultBlock:^(ALAssetsGroup *group) {
            [group addAsset:a];
            NSLog(@"added asset to NEW group");

        } failureBlock:^(NSError *error) {
            NSLog(@"e: %@", error);
        }];
    }
} failureBlock:^(NSError *error) {
    NSLog(@"e: %@", error);
}];



Best Answer-推荐答案


我已经和某人讨论过这个问题 HERE .

... when there's no photo album available, only part images are saved in a for loop. I tested the code and found that -addAssetsGroupAlbumWithName:resultBlock:failureBlock: will process asynchrony, this method will be dispatched several times when the album is not created (this might take some time).

In this case, you can just create a photo album before image adding process.

[self.assetsLibrary addAssetsGroupAlbumWithName:<your_custom_album_name>
                                    resultBlock:nil
                                   failureBlock:nil];

随意看看这个库:ALAssetsLibrary-CustomPhotoAlbum , 它可能对你有很多帮助;)

关于ios - 将照片保存到iOS相册,不要全部保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24836069/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4