在AGImagePickerController中确定所选照片

Baz*_*nga -1 iphone xcode ios

我正在使用AGImagePickerController.我很难搞清楚如何将选中的图像导入我iCarousel的另一个旋转木马.我知道success block其中包含所选图像.我似乎无法导入它awakeFromNib或将其放入数组中.

这是我调用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];
}
Run Code Online (Sandbox Code Playgroud)

在我的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];

    }
}
Run Code Online (Sandbox Code Playgroud)

然后我为我的旋转木马实现了这个:

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

Mic*_*ick 5

您应该能够查看info array使用NSLog语句的内容.查看该日志语句的内容会很有帮助.

我在这里找到了它,它可能会工作:

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

编辑:

您需要在用户选择后保存图像(在内存中或通过将其写入文件).如果要将它们写入文件,可以这样做:

[UIImageJPEGRepresentation(image, 1.0) writeToFile:jpgPath atomically:YES];
Run Code Online (Sandbox Code Playgroud)

如果AGImagePickerController代码在您的iCarousel类中,您只需将图像添加到您的images数组.你successBlock会看起来像这样:

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];
}
Run Code Online (Sandbox Code Playgroud)

最后,在您的iCarousel代码中,您需要从图像阵列或磁盘中读取图像(取决于您选择保存它们的位置).如果要将它们保存在内存中,则代码可能如下所示:

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

或者,如果您决定将图像保存到磁盘,则可以使用重新创建UIImage对象[UIImage imageWithContentsOfFile:filePath];.