【问题标题】:assetForURL with ios 5.0 doesn't work with deviceios 5.0 的assetForURL 不适用于设备
【发布时间】:2023-06-12 11:27:02
【问题描述】:
- (void)thumbnail:(NSNumber *)index{

    __block NSNumber *number = [NSNumber numberWithInt:[index intValue]];

    ALAssetsLibrary *library = [ALAssetsLibrary sharedALAssetsLibrary];

ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
{
        CGImageRef iref = [myasset thumbnail];
        if (iref) {
        [delegate thumbnailDidLoad:[UIImage imageWithCGImage:iref] withIndex:number];
        }
     NSLog(@"RESSSSSSSSSSSSSSSSSSSSSSSSSSSSSULT");
};

    ALAssetsLibraryAccessFailureBlock failureblock  = ^(NSError *myerror)
    {
        NSLog(@"Error, can't get image - %@",[myerror localizedDescription]);
    };
    NSString *mediaurl =  @"assets-library://asset/asset.JPG?id=5AF4118C-947D-4097-910E-47E19553039C&ext=JPG";

    NSURL *asseturl = [NSURL URLWithString:mediaurl];
    [library assetForURL:asseturl resultBlock:resultblock failureBlock:failureblock];
    NSLog(@"asseturl %@",asseturl);
}

这是我的代码,我的块有问题 - 它们在模拟器 5.0 下工作,但它们根本不能在设备下工作,它不会在断点处停止,NSLogs 也不起作用。使用模拟器一切正常。注意:CLAuthorizationStatus == kCLAuthorizationStatusAuthorized

【问题讨论】:

    标签: ios5 alassetslibrary


    【解决方案1】:

    确保整个函数 - (void)thumbnail:(NSNumber *)index... 从主线程执行,或者您确定用户已授权您的应用使用定位服务。如果你在后台调用它并且你还没有授权,那么永远不会提示用户批准,也不会调用结果块和失败块。

    【讨论】:

      【解决方案2】:

      从 iOS5 开始,assetForURL 可以异步工作。确保你打电话

          [delegate thumbnailDidLoad:[UIImage imageWithCGImage:iref] withIndex:number];
      

      在主线程上。这最容易通过在主队列上使用 dispatch_async 来完成。

      干杯,

      亨德里克

      【讨论】:

        最近更新 更多