2

当我在设备中运行程序并且它崩溃时,我遇到了上述错误,而它在模拟器中运行良好,我正在使用资产库加载图像并在滚动视图中显示。这是我的代码::

-

(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");
    }

    }


}

请纠正我哪里出错了。另外我想使用延迟加载加载图像,因为它首先迭代所有图像,然后在滚动视图中显示,因此当有大量图像时需要更多时间。另外,如果有任何其他链接,那么请发布它。感谢您提前回复。

4

0 回答 0