ALAsset Photo Library Image Performance Improvements When Its Loading Slow

前端 未结 2 1917
旧巷少年郎
旧巷少年郎 2020-12-04 00:51

Hi i\'ve got a problem with display an image on my scrollView.

At first i create new UIImageView with asset url:

-(void) findLargeImage:(NSNumber*)          


        
相关标签:
2条回答
  • 2020-12-04 01:30

    You are manipulating the view from another thread. You must use the main thread for manipulating the view.

    Add image to scrollView using:

    dispatch_async(dispatch_get_main_queue(), ^{
       [self.scrollView addSubview:itemToAdd];
    }
    

    or using:

    [self.scrollView performSelectorOnMainThread:@selector(addSubview:) withObject:itemToAdd waitUntilDone:NO];
    

    Please refer:

    1. NSObject Class Reference
    2. GCD
    0 讨论(0)
  • 2020-12-04 01:50

    ALAssetsLibrary block will execute in separate thread. So I suggest to do the UI related stuffs in main thread.

    To do this either use dispatch_sync(dispatch_get_main_queue() or performSelectorOnMainThread

    Some Important Notes:

    1. Use AlAsset aspectRatioThumbnail instead of fullResolutionImage for high performance

      Example:

     CGImageRef iref = [myasset aspectRatioThumbnail];
     itemToAdd.image = [UIImage imageWithCGImage:iref];
    

    Example:

    -(void) findLargeImage:(NSNumber*) arrayIndex
    {
     ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
     {
    
        CGImageRef iref = [myasset aspectRatioThumbnail];         
    
     dispatch_sync(dispatch_get_main_queue(), ^{
    
        itemToAdd = [[UIImageView alloc] initWithFrame:CGRectMake([arrayIndex intValue]*320, 0, 320, 320)];
        itemToAdd.image = [UIImage imageWithCGImage:iref];
        [self.scrollView addSubview:itemToAdd];
    
     });//end block
    
    
    };
    ALAssetsLibraryAccessFailureBlock failureblock  = ^(NSError *myerror)
    {
        NSLog(@"Cant get image - %@",[myerror localizedDescription]);
    };
    NSURL *asseturl = [NSURL URLWithString:[self.photoPath objectAtIndex:[arrayIndex intValue] ]];
    ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
    [assetslibrary assetForURL:asseturl 
                   resultBlock:resultblock
                  failureBlock:failureblock];
    
    }
    

    Also change the order of viewWillAppear()

    - (void) viewWillAppear:(BOOL)animated {
    
        self.scrollView.delegate = self;
        [self.view addSubview:self.scrollView];
        [self findLargeImage:self.actualPhotoIndex];
    
    }
    
    0 讨论(0)
提交回复
热议问题