0

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*) arrayIndex
{
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
{
    ALAssetRepresentation *rep;
    if([myasset defaultRepresentation] == nil) {
        return;
    } else {
        rep = [myasset defaultRepresentation];
    }
    CGImageRef iref = [rep fullResolutionImage];
    itemToAdd = [[UIImageView alloc] initWithFrame:CGRectMake([arrayIndex intValue]*320, 0, 320, 320)];
    itemToAdd.image = [UIImage imageWithCGImage:iref];
    [self.scrollView addSubview:itemToAdd];
};
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];

}

Where itemToAdd is a UIImageView define in interface:

__block UIImageView *itemToAdd;

And scrollView define as a property:

@property (nonatomic, strong) __block UIScrollView *scrollView;

Then in my viewWillAppear i do this:

- (void) viewWillAppear:(BOOL)animated {
    self.scrollView.delegate = self;
    [self findLargeImage:self.actualPhotoIndex];
    [self.view addSubview:self.scrollView];
}

But image doesnt appear, should i refresh self.view after add image to scrollView, or should do something else?

Shamsudheen TK
  • 30,739
  • 9
  • 69
  • 102
edzio27
  • 4,126
  • 7
  • 33
  • 48

2 Answers2

4

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];

}
Shamsudheen TK
  • 30,739
  • 9
  • 69
  • 102
0

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
Midhun MP
  • 103,496
  • 31
  • 153
  • 200
  • I dont know why but when i use first method it blocks my application, and i cant press any button on my view. When i use second method it works fine but it add itemToAdd subView to my self.view When i use this method to add itemToAdd to scrollView and and then add scrollView to self.view it doesnt work. – edzio27 Nov 22 '12 at 09:27
  • replace self.view with self.scrollView – Midhun MP Nov 22 '12 at 09:29
  • @edzio27: I think you used dispatch_sync not dispatch_async – Midhun MP Nov 22 '12 at 09:32
  • @edzio27: still have issues ? – Midhun MP Nov 22 '12 at 09:35
  • Yes, i use [self.scrollView performSelectorOnMainThread:@selector(addSubview:) withObject:itemToAdd waitUntilDone:NO]; in my block and [self.view addSubview:self.scrollView]; in my viewWillAppear and it still doesnt work. – edzio27 Nov 22 '12 at 09:43
  • check if the scrollView is nil or not – Midhun MP Nov 22 '12 at 09:47
  • It is not nil, i declare scrollView with method: - (UIScrollView *) scrollView { if(_scrollView == nil) { _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 80, 320, 320)]; return _scrollView; } } – edzio27 Nov 22 '12 at 09:53
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/19930/discussion-between-edzio27-and-midhun-mp) – edzio27 Nov 22 '12 at 10:08
  • @edzio27: please check my chat on http://chat.stackoverflow.com/rooms/19930/discussion-between-edzio27-and-midhun-mp – Midhun MP Nov 22 '12 at 10:56