0

I am very new to iOS and Objective-C, but I am trying to create an app that contains a component that takes all of the images from the camera roll and creates a slideshow from them. I believe the first step is to create an array from all of the images stored in the camera roll. Then I can create an animation from this array. Currently, I have a button, designated as "play", that when selected should display this slideshow as a UIImageView. However, my code does not work right now. Any help or suggestions would be greatly appreciated. Here is my code so far:

- (IBAction)play:(id)sender {
NSMutableArray *allPhotos= [[NSMutableArray alloc]init];
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {

    [group setAssetsFilter:[ALAssetsFilter allPhotos]];
    [group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
        if (result == nil) {
            return;
        }

        ALAssetRepresentation *representation = [result defaultRepresentation];
        UIImage *image = [UIImage imageWithCGImage:[representation fullScreenImage]];
        [allPhotos addObject:image];
    }];

} failureBlock: ^(NSError *error) {

    NSLog(@"No groups");
}];

UIImageView *animationImageView = [[UIImageView alloc] initWithFrame:CGRectMake(60, 95, 86, 193)];
animationImageView.animationImages = allPhotos;
animationImageView.animationDuration = 0.5;

[self.view addSubview:animationImageView];
[animationImageView startAnimating];

}

abovezero
  • 63
  • 2
  • 11
  • It would help if you told us what the problem is with the code you posted. – rmaddy Mar 10 '14 at 03:48
  • And you do realize that the enumeration is asynchronous so your attempt to use `allPhotos` with the `UIImageView` is being done long before the enumeration is complete. – rmaddy Mar 10 '14 at 03:49
  • 1
    And I do hope you realize that your app will crash due to too much memory usage for anyone with more than a few images in their camera roll. – rmaddy Mar 10 '14 at 03:50
  • The problem is that it is not returning any of the images from the camera roll or making the slideshow. If it will crash due to too much memory usage, what would be the best strategy for making a slideshow? Is it possible with the camera roll or would I have to save the photos some how within the app? – abovezero Mar 11 '14 at 01:14
  • Did you see my 2nd comment? – rmaddy Mar 11 '14 at 01:31

1 Answers1

0

See my answer here I posted:

Since ALAssetsLibrary is deprecated now and Photo Framework is new one. I made my own function in Objective C to get all photos from Camera Roll and store in NSArray and displayed in my Collectionview

 NSArray *imageArray;
 NSMutableArray *mutableArray;

    -(void)getAllPhotosFromCamera
    {
        imageArray=[[NSArray alloc] init];
        mutableArray =[[NSMutableArray alloc]init];

        PHImageRequestOptions *requestOptions = [[PHImageRequestOptions alloc] init];
        requestOptions.resizeMode   = PHImageRequestOptionsResizeModeExact;
        requestOptions.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
        requestOptions.synchronous = true;
        PHFetchResult *result = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:nil];

        NSLog(@"%d",(int)result.count);

        PHImageManager *manager = [PHImageManager defaultManager];
        NSMutableArray *images = [NSMutableArray arrayWithCapacity:[result count]];

        // assets contains PHAsset objects.

        __block UIImage *ima;
        for (PHAsset *asset in result) {
            // Do something with the asset

            [manager requestImageForAsset:asset
                               targetSize:PHImageManagerMaximumSize
                              contentMode:PHImageContentModeDefault
                                  options:requestOptions
                            resultHandler:^void(UIImage *image, NSDictionary *info) {
                                ima = image;

                                [images addObject:ima];
                            }];


        }

        imageArray = [images copy];  // You can direct use NSMutuable Array images
    }
Community
  • 1
  • 1
karan
  • 3,319
  • 1
  • 35
  • 44