Leena
Leena

Reputation: 2676

ALAssetLibrary and iOS 5.0

I have coded simple method to fetch data from assets library which is working fine on iOS4.3 but makes delay in fetching images in iOS5. What should i do to fasten fetching process on 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];

}

Upvotes: 2

Views: 603

Answers (1)

Aviel
Aviel

Reputation: 307

I already reported a bug to Apple (following a request of an Apple employee in the dev forums) on the performance degradation of assetForUrl in iOS5.

Background: The assetLibrary was refactored and it is now based on CoreData, on each assetForUrl call, the SDK actually opens a new SQLite connection (BAH...), causing a significant performance hit.

Temp solution: In my app I need to load 200 pics using assetForUrl. in IOS4 it took 100ms, in iOS5 around 5+ seconds. I found out that enumerating the entire library (around 1500 pics) and caching it in a URL-->ASSET dictionary, takes around 3 seconds. I'm using this technique for now. Watch out for stale assets if you hold on to them and changes to the library occur.

Upvotes: 2

Related Questions