ALAssetsLibrary和iOS 5.0

时间:2011-12-06 12:51:32

标签: iphone ios4 ios5

我编写了一个简单的方法来从资源库中获取数据,这在iOS4.3上工作得很好,但是在iOS5中提取图像会有延迟。我该怎么做才能在iOS 5上加强抓取过程。

-(void)setImages
{
    int count =0;
    int photoNumber = [[templateDictionary objectForKey:@"ElementsOnPage"] intValue]; 
    for (int i=currentCount; count<photoNumber; i++) {
       [self data:count+1 count:i];
        count++;
    }
 }


-(void)data:(int)photoNumber count:(int)currentCount 
{  
NSURL *url;
UIImageView *firstImageView = [[UIImageView alloc]init];
CGFloat x,y,wid,h;
float ang;
 if (currentCount>=[ImageURLArray count]) {
    [firstImageView release];
    return;
}
else
{
    url = [NSURL URLWithString:[ImageURLArray objectAtIndex:currentCount]];
    switch (photoNumber) {
        case 1:
        {

            x = [[templateDictionary  objectForKey:@"FirstElement_X"]floatValue];
            y=[[templateDictionary objectForKey:@"FirstElement_Y"]floatValue];
            wid = [[templateDictionary objectForKey:@"FirstElement_Width"]floatValue];
            h=[[templateDictionary objectForKey:@"FirstElement_Height"]floatValue];
            ang =[[templateDictionary objectForKey:@"FirstElement_Angle"]floatValue];
            firstImageView.tag = 1+10;
            //FirstImage

        }
            break;
        case 2:
        {
            x = [[templateDictionary objectForKey:@"SecondElement_X"]floatValue];
            y=[[templateDictionary objectForKey:@"SecondElement_Y"]floatValue];
            wid = [[templateDictionary objectForKey:@"SecondElement_Width"]floatValue];
            h=[[templateDictionary objectForKey:@"SecondElement_Height"]floatValue];
            ang =[[templateDictionary objectForKey:@"SecondElement_Angle"]floatValue];
            firstImageView.tag=2+10;
            //SecondImage

        }
            break;
        case 3:
        {
            x = [[templateDictionary objectForKey:@"ThirdElement_X"]floatValue];
            y=[[templateDictionary objectForKey:@"ThirdElement_Y"]floatValue];
            wid = [[templateDictionary objectForKey:@"ThirdElement_Width"]floatValue];
            h=[[templateDictionary objectForKey:@"ThirdElement_Height"]floatValue];
            ang =[[templateDictionary objectForKey:@"ThirdElement_Angle"]floatValue];
            firstImageView.tag = 3+10;
            //ThirdImage
        }
            break;
        default:
            break;
    }
    [firstImageView setFrame:CGRectMake(x, y, wid, h)];

    dispatch_async(dispatch_get_main_queue(), ^
                   {
                       NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
                       ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
                       // Enumerate just the photos and videos group by using ALAssetsGroupSavedPhotos.

                       [library assetForURL:url 

                                resultBlock:^(ALAsset* asset) 
                        {

                            UIImage* img = [UIImage imageWithCGImage:[asset.defaultRepresentation fullResolutionImage]];
                            [firstImageView setImage:img];

                        }  
                               failureBlock:^(NSError* error) 
                        {
                            NSLog(@"error requesting asset");
                        }
                        ];

                       [library release];   

                       // Group enumerator Block
                       [pool release];
                   });


    if ([[contentType objectAtIndex:currentCount]isEqualToString:@"1"]) {
        UIButton *videoImage = [[UIButton alloc]initWithFrame:CGRectMake((firstImageView.frame.size.width/2)-25,(firstImageView.frame.size.height/2)-25,50,50)];
        videoImage.transform = CGAffineTransformMakeRotation(ang*(3.14/180));
        [videoImage setBackgroundImage:[UIImage imageNamed:@"videothumb.png"] forState:UIControlStateNormal];
        [videoImage addTarget:self action:@selector(PlayMusicOnClickofButton:) forControlEvents:UIControlEventTouchUpInside];
        [firstImageView addSubview:videoImage];
        videoImage.tag  = currentCount+1000;
        [videoImage release];
    }
}
firstImageView.transform = CGAffineTransformMakeRotation(ang*(3.14/180));
firstImageView.userInteractionEnabled = YES;
[coverImageView addSubview:firstImageView];
[coverImageView setImage:[UIImage imageNamed:innerBackground]];

[firstImageView release];

}

1 个答案:

答案 0 :(得分:2)

我已经向Apple报告了一个关于iOS5中assetForUrl性能下降的问题(根据开发论坛中的Apple员工的请求)。

背景:assetLibrary已经重构,现在它基于CoreData,在每次assetForUrl调用时,SDK实际上会打开一个新的SQLite连接(BAH ...),从而导致显着的性能损失。

临时解决方案:在我的应用中,我需要使用assetForUrl加载200张图片。在IOS4中花费了100毫秒,在iOS5中大约需要5秒。我发现枚举整个库(大约1500个图片)并将其缓存在一个URL - &gt; ASSET字典中,需要大约3秒钟。我现在正在使用这种技术。如果你坚持下去并且发生了对库的更改,请注意过时的资产。