gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 16:41:38 27 4
gpt4 key购买 nike

我有一个返回 ALAsset 数据的方法。我为一个又一个 Assets 调用此方法,以从它们获取 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 中暂停和取消暂停调试器,代码会向前移动。

最佳答案

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

关于ios - 请求 ALAssetRepresentation 的 size 属性时代码卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23823349/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com