在iOS 8上打破的AssetsLibrary框架 [英] AssetsLibrary framework broken on iOS 8

查看:91
本文介绍了在iOS 8上打破的AssetsLibrary框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在iOS 8上遇到了一个问题,资产库框架似乎是iOS 8中的一个错误。如果我创建了一个名为MyMedia的专辑,然后将其删除,那么当我再次尝试创建专辑时,下面这段代码返回'nil'表示专辑'MyMedia'存在,即使它没有,因为我使用'Photos'应用删除了它。

I have run into an issue on iOS 8 with the Assets Library framework that appears to be a bug in iOS 8. If I create an album called 'MyMedia' and then delete it, then when I try to create the album again, this chunk of code below returns 'nil' indicating that the album 'MyMedia' exists even though it does not because I deleted it using the 'Photos' app.

__block ALAssetsGroup *myGroup = nil;
__block BOOL addAssetDone = false;
NSString *albumName = @"MyMedia";
[assetsLib addAssetsGroupAlbumWithName:albumName
                           resultBlock:^(ALAssetsGroup *group) {
                               myGroup = group;
                               addAssetDone = true;
                           } failureBlock:^(NSError *error) {
                               NSLog( @"failed to create album: %@", albumName);
                               addAssetDone = true;
                           }];

while (!addAssetDone) {
    [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.05f]];
}
return myGroup; // returns nil if group has previously been created and then deleted

创建品牌时,此方法也适用新专辑MyMedia2。还有其他人遇到过这个问题并知道解决方法或解决方案吗?是唯一能够转移到新照片框架的解决方案还是我在这里做错了什么?请注意,此代码始终适用于iOS7.X

This same method works when creating a brand new album 'MyMedia2.' Has anyone else experienced this issue and know of a workaround or solution? Is the only solution to move to the new 'Photos' framework or am I doing something incorrect here? Note that this code always works on iOS7.X

实际上,重现此问题的步骤如下 - >
1.卸载拍摄照片的应用程序将它们保存到自定义相册
2.在iOS照片下删除已保存照片的自定义相册
3.安装您的应用
4.如果您使用应用程序拍照或录制视频它不会创建它们或存储它们。如果您查看iOS相册,则自定义相册不存在,并且不存在使用该应用拍摄的所有照片/视频。

Actually the steps to reproduce this problem are as follows -> 1. Uninstall your app that takes photos and saves them to a custom album 2. Under iOS Photos delete the custom album that has saved photos in it 3. Install your app 4. If you take pictures or record videos with the app it does not create them or store them. If you look under iOS Photos albums the custom album one does not exist and none of the pictures/videos taken with the app exist.

推荐答案

我之前的回答是不正确的。我还没有真正测试过它。我终于弄清楚要做什么,这很困难,但我得到了它的工作。这就是我必须要做的就是让我的应用程序在iOS 7.xX和iOS 8.Xx上运行并创建一个以前被应用程序删除的自定义相册 - >

My previous answer was incorrect. I had not really tested it out. I did finally figure out what had to be done and it was difficult but I got it to work. This is what I had to do to get my app to run on both iOS 7.x.X and iOS 8.X.x and create a custom album that had been previously deleted by the app -->


  1. 我写了两个代码块:一个使用iOS 8.xx上的Photos框架,另一个使用iOS 7.xx上的AssetsLibrary框架

  1. I wrote two chunks of code: one that uses the Photos framework on iOS 8.x.x and one that uses the AssetsLibrary framework on iOS 7.x.x

Sp可以在两个iOS版本上运行,我将应用程序链接到Photos框架,但后来将其从必需更改为可选,因此不会在iOS 7.xx上加载

Sp the app could run on both iOS versions, I linked the app to the Photos framework but then changed it from required to optional so it would not be loaded on iOS 7.x.x

因为在iOS 7.xx上无法在运行时直接调用Photos框架代码,所以我必须更改它以便加载类,函数(和块!)在运行时动态

Because the Photos framework code could not be called directly at runtime on iOS 7.x.x, I had to change it so it loaded the classes, functions (and blocks!) dynamically at runtime

这是在iPhone上运行时工作的代码块。这也应该在模拟器中工作 - >

Here is the code chunk that works when running on an iPhone. This should work in the simulator too -->

// PHPhotoLibrary_class will only be non-nil on iOS 8.x.x
Class PHPhotoLibrary_class = NSClassFromString(@"PHPhotoLibrary");

if (PHPhotoLibrary_class) {

   /**
    *
    iOS 8..x. . code that has to be called dynamically at runtime and will not link on iOS 7.x.x ...

    [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
        [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:title];
    } completionHandler:^(BOOL success, NSError *error) {
        if (!success) {
            NSLog(@"Error creating album: %@", error);
        }
    }];
    */

    // dynamic runtime code for code chunk listed above            
    id sharedPhotoLibrary = [PHPhotoLibrary_class performSelector:NSSelectorFromString(@"sharedPhotoLibrary")];

    SEL performChanges = NSSelectorFromString(@"performChanges:completionHandler:");

    NSMethodSignature *methodSig = [sharedPhotoLibrary methodSignatureForSelector:performChanges];

    NSInvocation* inv = [NSInvocation invocationWithMethodSignature:methodSig];
    [inv setTarget:sharedPhotoLibrary];
    [inv setSelector:performChanges];

    void(^firstBlock)() = ^void() {
        Class PHAssetCollectionChangeRequest_class = NSClassFromString(@"PHAssetCollectionChangeRequest");
        SEL creationRequestForAssetCollectionWithTitle = NSSelectorFromString(@"creationRequestForAssetCollectionWithTitle:");
        [PHAssetCollectionChangeRequest_class performSelector:creationRequestForAssetCollectionWithTitle withObject:albumName];

    };

    void (^secondBlock)(BOOL success, NSError *error) = ^void(BOOL success, NSError *error) {
       if (success) {
           [assetsLib enumerateGroupsWithTypes:ALAssetsGroupAlbum usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
               if (group) {
                   NSString *name = [group valueForProperty:ALAssetsGroupPropertyName];
                   if ([albumName isEqualToString:name]) {
                       groupFound = true;
                       handler(group, nil);
                   }
               }
           } failureBlock:^(NSError *error) {
               handler(nil, error);
           }];
       }

       if (error) {
           NSLog(@"Error creating album: %@", error);
           handler(nil, error);
       }
   };

   // Set the success and failure blocks.
   [inv setArgument:&firstBlock atIndex:2];
   [inv setArgument:&secondBlock atIndex:3];

   [inv invoke];

}
else {   
   // code that always creates an album on iOS 7.x.x but fails
   // in certain situations such as if album has been deleted
   // previously on iOS 8...x. .              
   [assetsLib addAssetsGroupAlbumWithName:albumName
       resultBlock:^(ALAssetsGroup *group) {
       handler(group, nil);
   } failureBlock:^(NSError *error) {
       NSLog( @"Failed to create album: %@", albumName);
       handler(nil, error);
   }];
}

这篇关于在iOS 8上打破的AssetsLibrary框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆