我在设备中运行程序时遇到上述错误并且崩溃,而它在模拟器中工作正常,我使用资源库加载图像并在滚动视图中显示。 这是我的代码::
-
(void)usingAssets{
x=0;
y=0;
w=320;
h=460;
void (^assetEnumerator)( ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop) {
if(result != NULL) {
[app hideLoading];
[assets addObject:result];
NSLog(@"assets count: %i", assets.count);
NSString *assetType = [result valueForProperty:ALAssetPropertyType];
if ([assetType isEqualToString:ALAssetTypePhoto]) {
NSLog(@"Photo Asset");
[self success];
}
}
else {
[app hideLoading];
NSLog(@"AssetEnum: result nil or end of list");
}
};
void (^assetGroupEnumerator)( ALAssetsGroup *, BOOL *) = ^(ALAssetsGroup *group, BOOL *stop) {
if(group != nil) {
[group enumerateAssetsUsingBlock:assetEnumerator];
}
else {
NSLog(@"GroupEnum: group nil or end of list..");
}
[assets retain];
};
ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos
usingBlock:assetGroupEnumerator
failureBlock: ^(NSError *error) {
NSLog(@"Failure: %@", error.description);
}];
// [library release];
isVisited=YES;
}
-(void)success{
dispatch_async(dispatch_get_main_queue(), ^(void){
for(int i=0;i<[assets count];i++){
asset = [assets objectAtIndex:i];
UIImage *images = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage] scale:1.0 orientation:[[asset valueForProperty:@"ALAssetPropertyOrientation"] intValue]];
imageview =[[UIImageView alloc]init];
imageview.frame=CGRectMake(x, y, w, h);
[imageview setImage:images];
[scrollview addSubview:imageview];
x = x + w ;
}
});
}
#pragma mark:----scroll view methods
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGPoint scrollOffset=scrollView.contentOffset;
///at any time, it will have only 3 pages loaded- previous, current and next
if(pageno < ((int)scrollOffset.x/320))
{
//unload
if(pageno>1)[self unloadPreviousPage:pageno-2];
//load the next page
[self loadScrollViewWithPage:((int)scrollOffset.x/320)+1];
}
else if(pageno > ((int)scrollOffset.x/320))
{
//unload
if(pageno<(kNumberOfPages-2))[self unloadPreviousPage:pageno+2];
//load back the previous page
[self loadScrollViewWithPage:((int)scrollOffset.x/320)-1];
}
pageno=scrollOffset.x/320;
}
-(void)unloadPreviousPage:(int)index
{
for(int i=index*4;i<(index+1)*4;i++)
{
[[scrollview viewWithTag:i+1] removeFromSuperview];
}
}
- (void)loadScrollViewWithPage:(int)page {
if (page < 0)
return;
if (page >= kNumberOfPages)
return;
pageno=page;
pgControl.currentPage=pageno-1;
if(isVisited==NO){
[NSThread detachNewThreadSelector:@selector(usingAssets) toTarget:self withObject:nil];
assets = [[NSMutableArray alloc] init];
for (ALAsset* a in assets){
NSLog(@"Item in asset");
}
}
}
请在我错误的地方纠正我。我还想使用延迟加载来加载图像,因为它首先迭代所有图像然后在滚动视图中显示,因此当有大量图像时需要更多时间。 如果还有其他链接,请发布。 感谢您的回复。