我已经设置了一些功能。但我迷失在这里去哪里。一旦我知道如何实际使用图像,我可能会弄清楚与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
}
}
.

有帮助吗?

解决方案

假设您已从视图控制器中调用了它,使用您创建的按钮,为什么不仅在视图控制器上添加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;
.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top