0

我已经设置了一些功能。但我不知道从这里去哪里。一旦我知道如何实际使用图像,我可能会弄清楚如何处理 facebook。我尝试将图像保存到 NSDictionary 中,然后重定向到不同的视图控制器,但它不允许我从 imagePickerController 方法中重定向。所以有人知道如何使用从相机胶卷中选择的图像吗?

我的下一个想法是将它保存在 NSDictionary 中,然后只用一个语句检查 NSdictionary 的值是否发生了变化,但这根本不是一种有效的方法。

在下面编辑以包括提供的答案,但没有任何反应,没有图像显示或任何东西。我错过了什么?

- (void) useCameraRoll
{
if ([UIImagePickerController isSourceTypeAvailable:
     UIImagePickerControllerSourceTypeSavedPhotosAlbum])
{
    UIImagePickerController *imagePicker =
    [[UIImagePickerController alloc] init];
    imagePicker.delegate = self;
    imagePicker.sourceType =
    UIImagePickerControllerSourceTypePhotoLibrary;
    imagePicker.mediaTypes = [NSArray arrayWithObjects:
                              (NSString *) kUTTypeImage,
                              nil];
    imagePicker.allowsEditing = NO;
    [self presentModalViewController:imagePicker animated:YES];
    [imagePicker release];
    newMedia = NO;
}
}

-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *mediaType = [info
                       objectForKey:UIImagePickerControllerMediaType];
[self dismissModalViewControllerAnimated:YES];
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
    UIImage *image = [info
                      objectForKey:UIImagePickerControllerOriginalImage];


    NSLog(@"image:%@",image);


    displayPhoto.image = image;
    [displayPhoto setFrame:CGRectMake(0, 0, 320, 480)];
    [[self view] addSubview:displayPhoto];

}
else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie])
{
    // Code here to support video if enabled
}
}
4

1 回答 1

1

假设您已经使用您创建的按钮从视图控制器调用它,为什么不直接将 UIImageView 添加到您的视图控制器上呢?将其称为 myPhotoImageView 或类似的名称,然后在 didFinishPickingMediaWithInfo 方法中,只需在后面添加以下代码行

UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];

作为

myPhotoImageView.image = image;

要调整图像视图的大小以使外观看起来不错,请执行此操作。

CGSize size = image.size;
CGRect photoFrame;
if (size.width > size.height)
{
    // Landscape
    CGFloat scaleFactor = 320 / size.width;
    photoFrame = CGRectMake(0, 0, 320, size.height*scaleFactor);
}
else
{
    // Portrait
    CGFloat scaleFactor = 320 / size.height;
    photoFrame = CGRectMake(0, 0, size.width*scaleFactor, 320);
}
myPhotoImageView.frame = photoFrame;
于 2012-10-02T08:33:18.240 回答