0

UIImageVIew我用 placeholder.png 图像初始化。当我尝试使用UIImagePicker控制器中的图像更改 placeholder.png 时,它不会改变。

employeePhoto=[[UIImageView alloc] init];
[employeePhoto setFrame:CGRectMake(50, 50, 150, 150)];
[employeePhoto setImage:[UIImage imageNamed:@"placeholder.png"]];
[self.view addSubview:employeePhoto];

UIImagePickerController委托中

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *image=[info valueForKey:@"UIImagePickerControllerOriginalImage"];

    [employeePhoto setImage:nil]; // or employeePhoto.image=nil;
    [employeePhoto setImage:image];

    [picker dismissViewControllerAnimated:YES completion:NULL];
}

但是在解雇选择器后UIImageView不会改变。我也试过用UIButton而不是UIImageView. UIImageViewUIButton声明为@property(nonatomic, retain),我使用的是 xcode 4.6.1。

请帮我解决这个问题...

提前致谢.....

4

5 回答 5

1

试试这样:

- (void) imagePickerController: (UIImagePickerController*) picker
 didFinishPickingMediaWithInfo: (NSDictionary*) info
{
    UIImage*image = [info valueForKey: UIImagePickerControllerOriginalImage];
    [employeePhoto setImage: image];

    dispatch_async(dispatch_get_main_queue(), ^{
        [picker dismissViewControllerAnimated: YES 
                                   completion: NULL];
    });
}
于 2013-06-19T07:02:48.570 回答
1

请尝试以下代码:

@property(nonatomic,retain) UIImageView *employeePhoto;
@property(nonatomic,retain) UIImagePickerController *imagePicker;


/* Alloc and Init UIImageView and AddSubview to UIView */

self.employeePhoto = [[UIImageView alloc] init] autorelease];
[self.employeePhoto setFrame:CGRectMake(50, 50, 150, 150)];
[self.employeePhoto setImage:[UIImage imageNamed:@"placeholder.png"]];
[self.view addSubview:self.employeePhoto];

 /* Alloc and Init UIImagePickerController in ViewDidLoad */ 

 self.imagePicker = [[UIImagePickerController alloc] init] autorelease];
 self.imagePicker.delegate = self;
 self.imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
 self.imagePicker.mediaTypes =[UIImagePickerController availableMediaTypesForSourceType:self.imagePicker.sourceType];
 [self presentViewController:self.imagePicker animated:YES completion:nil];

/* Write UIImagePickerController Delegate Method */ 

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *original = [info objectForKey:UIImagePickerControllerOriginalImage];
    self.employeePhoto.image = original;
    [picker dismissViewControllerAnimated:YES completion:nil];

}

请尝试此代码,如果您遇到任何问题,请告诉我。谢谢。

于 2013-06-19T06:41:19.127 回答
0

如果你做了

piker.allowsImageEditing = NO;

然后

UIImage *image=[info objectForKey:UIImagePickerControllerOriginalImage];

如果你做了

piker.allowsImageEditing = YES;

然后使用

UIImage *image=[info objectForKey:UIImagePickerControllerEditedImage];

也许它会帮助你。但首先检查您是否会在 didFinishPickingMediaWithInfo 中获得图像。

于 2013-06-19T05:03:38.613 回答
0

使用-(void)imagePickerController:(UIImagePickerController *)picker...方法,在使您的 imageview = nil 之后。

再次分配图像视图,如下所示...

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage     *image=[info valueForKey:@"UIImagePickerControllerOriginalImage"];

    [employeePhoto setImage:nil]; // or employeePhoto.image=nil;
    employeePhoto=[[UIImageView alloc] init];
    [employeePhoto setFrame:CGRectMake(50, 50, 150, 150)];
    [employeePhoto setImage:image];

    [picker dismissViewControllerAnimated:YES completion:NULL];
}

让我知道它是否有效!

快乐编码!!!

于 2013-06-19T06:16:24.837 回答
0

用这个:

UIImage *image=[info objectForKey:UIImagePickerControllerOriginalImage];

代替

UIImage *image=[info valueForKey:@"UIImagePickerControllerOriginalImage"];

并在 viewDidLoad 中分配employeePhoto

于 2013-06-19T04:56:58.530 回答