Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
389 views
in Technique[技术] by (71.8m points)

ios5 - ALAssetsLibrary delete ALAssetsGroup / ALAsset

I have created "photos album" from my App, using IOS AssetsLibrary.

Reading ALAssetsLibrary,ALAssetsGroup and ALAsset documentations, i have seen methods to "addAsset","addAssetsGroupAlbumWithName".

Is there a way to delete PROGRAMMATICALLY my ALAssetsGroup and ALAsset. (the property 'editable' suppose to be TRUE because i create this data).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can only delete the ALAsset which is created by your app with document API [ALAsset setImageData:metadata:completionBlock:] (But I have not found any API to delete a ALAssetGroup).

1). Add an image "photo.jpg" to your project 2). Save an image to asset library:

ALAssetsLibrary *lib = [ALAssetsLibrary new];
UIImage *image = [UIImage imageNamed:@"photo.jpg"];
[lib writeImageToSavedPhotosAlbum:image.CGImage metadata:@{} completionBlock:^(NSURL *assetURL, NSError *error) {
    NSLog(@"Write image %@ to asset library. (Error %@)", assetURL, error);
}];

3). Go to default gallery, you will find photo.jpg in your "Saved Photos" album.

4). Delete this image from asset library:

ALAssetsLibrary *lib = [ALAssetsLibrary new];
[lib enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
    [group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop) {
        if(asset.isEditable) {
            [asset setImageData:nil metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {
                NSLog(@"Asset url %@ should be deleted. (Error %@)", assetURL, error);
            }];
        }
    }];
} failureBlock:^(NSError *error) {

}];

5). Go to default gallery, you will find photo.jpg has already been deleted.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...