Determine selected photos in AGImagePickerControll

2019-09-19 11:00发布

问题:

I'm using AGImagePickerController. Im having a hard time figuring out how to import the images selected into my iCarousel which is in another carousel. I know that the success block in it contains the selected image. I can't seem to import it in my awakeFromNib or putting it in an array.

here is my code that calls the AGImagePickerController:

 -(IBAction) cameraRoll {AGImagePickerController *imagePickerController = [[AGImagePickerController alloc] initWithFailureBlock:^(NSError *error) {

        if (error == nil)
        {
            NSLog(@"User has cancelled.");
            [self dismissModalViewControllerAnimated:YES];
        } else
        {     
            NSLog(@"Error: %@", error);

            // Wait for the view controller to show first and hide it after that
            double delayInSeconds = 0.5;
            dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
            dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
                [self dismissModalViewControllerAnimated:YES];
            });
        }

        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];

    } andSuccessBlock:^(NSArray *info) {
        NSLog(@"Info: %@", info);
        [self dismissModalViewControllerAnimated:YES];

        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
    }];

    [self presentModalViewController:imagePickerController animated:YES];
    [imagePickerController release];
}

In my awakeFromNib:

- (void)awakeFromNib
{    
    if (self) {

        self.images = [NSMutableArray arrayWithObjects:@"111.jpg",
                       @"112.jpg",
                       @"113.jpg",
                       @"114.jpg",
                       @"115.jpg",
                       @"116.jpg",
                       @"117.jpg",
                       @"118.png",
                       @"119.jpg",
                       @"120.jpg",
                       nil];

    }
}

Then I implement this for my carousel :

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index
{
    //create a numbered view
    UIView *view = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[images objectAtIndex:index]]];
    return view;
}

回答1:

You should be able to look at the contents of the info array using your NSLog statement. It would have been helpful to see the contents of that log statement.

I found this here and it might work:

for (i = 0; i < info.count; i++) {
     ALAssetRepresentation *rep = [[info objectAtIndex: i] defaultRepresentation];
     UIImage * image  = [UIImage imageWithCGImage:[rep fullResolutionImage]];
}

Edit:

You need to save the images (either in memory or by writing them to files) after the user selects them. If you want to write them to files, you can do that like so:

[UIImageJPEGRepresentation(image, 1.0) writeToFile:jpgPath atomically:YES];

If the AGImagePickerController code is in your iCarousel class, you can just add the images to your images array. Your successBlock would look something like this:

self.images = [NSMutableArray new];

for (i = 0; i < info.count; i++) {
     ALAssetRepresentation *rep = [[info objectAtIndex: i] defaultRepresentation];
     UIImage * image  = [UIImage imageWithCGImage:[rep fullResolutionImage]];
     [self.images addObject:image];
}

And finally, in your iCarousel code you need to read the images either from your images array or from the disk (depending on where you chose to save them). If you are saving them in memory your code could look like this:

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index
{
    return [[UIImageView alloc] initWithImage:[self.images objectAtIndex:index]];
}

Or, if you decided to save the images to disk then you could recreate the UIImage objects using [UIImage imageWithContentsOfFile:filePath];.



标签: iphone ios xcode