-1

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

1 回答 1

5

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

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

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

于 2012-05-26T16:01:22.487 回答