2
我打算在不影響程序或主線程執行的情況下,將所有照片庫圖像URL添加到單獨線程的NSMutableArray中。在單獨線程上運行ALAssetLibrary塊
我需要在應用程序背景上執行進程,而其他操作正在進行。
因此,我在代碼以及我的View Controller類的ViewDidLoad函數中編寫了下面的代碼,代碼如下:我的應用程序委託類「didFinishLaunchingWithOptions」。
但是,其他功能在ALAsset塊正在執行時不工作。
但方法「crcGeneration」正在工作,不會干擾其他功能。正如在下面的代碼中可以看到的,在獲得所有照片庫URL之後,我在同一個線程中調用了ALAsset塊內部的方法「crcGeneration」。
任何人都可以告訴我一個很好的方法來解決這個問題嗎?
OnViewDidLoad:
//Calling the getAllURLofPhotLibraryImages Function on a seperate Thread.
[NSThread detachNewThreadSelector:@selector(getAllURLofPhotLibraryImages) toTarget:self withObject:nil];
Function for Getting all image URL's of PhotoLibrary.
**-(void)getAllURLofPhotLibraryImages**
{
urlStoreArray = [[NSMutableArray alloc] init]; //global array
void (^assetEnumerator)(struct ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop)
{
if(result != NULL)
{
// Here storing the asset's image URL's in NSMutablearray urlStoreArr
NSURL *url = [[result defaultRepresentation] url];
[urlStoreArray addObject:url];
}
};
void (^assetGroupEnumerator)(struct ALAssetsGroup *, BOOL *) = ^(ALAssetsGroup *group, BOOL *stop)
{
if(group != nil)
{
[group enumerateAssetsUsingBlock:assetEnumerator];
}
else
{
**[self crcGeneration];** //Genearting CRC for PhotoLibrary Images using urlStoreArray after got all the Photo Library URL's .
}
};
ALAssetsLibrary* assetslibrary = [[[ALAssetsLibrary alloc] init] autorelease];
[assetslibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:assetGroupEnumerator failureBlock: ^(NSError *error) { NSLog(@"Failure");
}];
}