请求ALAssetRepresentation的size属性时代码卡住了

时间:2014-05-23 07:25:47

标签: ios iphone alassetslibrary alasset

我有一个返回ALAsset数据的方法。我将这种方法称为一种资产,然后从中获取NSData并将其上传到服务器。每4或5次调用后,代码将停留在下面的rep.size调用中。当我暂停并在XCode中恢复执行时,它再次开始工作。我完全被难过了,任何帮助都会受到赞赏。

澄清:死锁/代码卡在ALAssetsLibrary代码中,而不是在我的代码中。

附加信息:我只有一个ALAssetsLibrary实例,我确保它没有被任何其他线程使用。

+(void)getDataFromAssetURL:(NSString *) myImageURL ofType:(enum ImageType)imageType andPerformBlock:(NSDataBlock)block blocking:(BOOL)blocking
{
    NSConditionLock * albumReadLock = nil;
    if (blocking) {

        albumReadLock = [[NSConditionLock alloc] initWithCondition:PENDING];
    }
    @autoreleasepool {
        @try {

            NSURL *str = [[NSURL alloc] initWithString: myImageURL];

            ALAsset * asset = [[AppManager sharedInstance].assetObjectCache objectForKey:str];
            if (asset && NO) {
                block( [Util getDataFromAsset:asset ofType:imageType] );

                // notifies the lock that "all tasks are finished"
                [albumReadLock lock];
                [albumReadLock unlockWithCondition:ALLFINISHED];
            }
            else {
                ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
                {
                    @autoreleasepool {
                        @try {


                            [[AppManager sharedInstance].assetObjectCache setObject:myasset forKey:str];

                            NSData * assetData = [Util getDataFromAsset:myasset ofType:imageType];

                            block(assetData);

                            // notifies the lock that "all tasks are finished"
                            [albumReadLock lock];
                            [albumReadLock unlockWithCondition:ALLFINISHED];



                        }
                        @catch (NSException *exception) {
                            block(nil);
                            // important: notifies lock that "all tasks finished" (even though they failed)
                            [albumReadLock lock];
                            [albumReadLock unlockWithCondition:ALLFINISHED];
                        }
                        @finally {

                        }
                    }

                };

                ALAssetsLibraryAccessFailureBlock failureblock  = ^(NSError *myerror)
                {
                    block(nil);
                    // important: notifies lock that "all tasks finished" (even though they failed)
                    [albumReadLock lock];
                    [albumReadLock unlockWithCondition:ALLFINISHED];
                };



                if(str)
                {
                    NSURL *asseturl = str;

                    [[AppManager sharedInstance].assetslibrary assetForURL:asseturl
                                                               resultBlock:resultblock
                                                              failureBlock:failureblock];

                }
                else {
                    block(nil);
                    // notifies the lock that "all tasks are finished"
                    [albumReadLock lock];
                    [albumReadLock unlockWithCondition:ALLFINISHED];
                }
            }


        }
        @catch (NSException *exception) {
            block(nil);
            // notifies the lock that "all tasks are finished"
            [albumReadLock lock];
            [albumReadLock unlockWithCondition:ALLFINISHED];
        }
        @finally {

        }
    }

    if (blocking) {
        // non-busy wait for the asset read to finish (specifically until the condition is "all finished")
        [albumReadLock lockWhenCondition:ALLFINISHED];
        [albumReadLock unlock];

    }
}


+(NSData *)getDataFromAsset:(ALAsset *)myasset ofType:(enum ImageType)imageType
{
    ALAssetRepresentation *rep = [myasset defaultRepresentation];

    CGImageRef iref = nil;
    NSData *assetData = nil;

    if (imageType == FULL_RESOLUTION) {
        Byte *buffer = (Byte*)malloc(rep.size);
        NSUInteger buffered = [rep getBytes:buffer fromOffset:0.0 length:rep.size error:nil];
        assetData = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES];
    }
    else if (imageType == LARGE_IMAGE){
        iref = [rep fullScreenImage];
        UIImage * uiimage = [UIImage imageWithCGImage:iref];
        ////NSLog(@"%f %f", uiimage.size.width, uiimage.size.height);
        assetData = UIImageJPEGRepresentation(uiimage, 1.0f);
    }
    else if (imageType == SQUARE_THUMB){
        iref = [myasset thumbnail];
        assetData = UIImageJPEGRepresentation([UIImage imageWithCGImage:iref], 1.0f);
    }
    else if (imageType == PERSPECTIVE_THUMB){
        iref = [myasset aspectRatioThumbnail];
        assetData = UIImageJPEGRepresentation([UIImage imageWithCGImage:iref], 1.0f);
    }

    return assetData;
}

新信息:仅在特定设备上发生问题。如上所述,如果我在XCode中暂停和取消暂停调试器,则代码会向前移动。

1 个答案:

答案 0 :(得分:0)

如果从主线程启动此代码块,则会导致ALAsset代码出现死锁。您必须首先进入后台线程,因为ALAssetsLibrary需要能够在主线程上执行代码,并且如果主线程以您上面概述的方式被阻止,那么它将永远等待这样做。

相关问题