在AGImagePickerController中确定所选照片

时间:2012-04-11 08:40:21

标签: iphone ios xcode

我正在使用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];
}

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

    }
}

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

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

1 个答案:

答案 0 :(得分:5)

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

我找到了这个here,它可能有效:

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

修改

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

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

如果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];
}

最后,在您的iCarousel代码中,您需要从图像阵列或磁盘中读取图像(取决于您选择保存它们的位置)。如果你将它们保存在内存中 你的代码看起来像这样:

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

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