Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/96.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 如何从uiimageview操作uibutton中删除图像_Iphone_Ios - Fatal编程技术网

Iphone 如何从uiimageview操作uibutton中删除图像

Iphone 如何从uiimageview操作uibutton中删除图像,iphone,ios,Iphone,Ios,我使用placeholder.png图像初始化UIImageVIew。当我尝试使用UIImagePickercontroller中的图像更改placeholder.png时,它不会更改 employeePhoto=[[UIImageView alloc] init]; [employeePhoto setFrame:CGRectMake(50, 50, 150, 150)]; [employeePhoto setImage:[UIImage imageNamed:@"placeholder.pn

我使用placeholder.png图像初始化
UIImageVIew
。当我尝试使用
UIImagePicker
controller中的图像更改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
不会改变。我还尝试了使用
ui按钮,而不是
UIImageView
UIImageView
UIButton
声明为
@属性(非原子,保留)
,我使用的是xcode 4.6.1

请帮我解决这个问题

提前感谢….

使用此:

UIImage *image=[info objectForKey:UIImagePickerControllerOriginalImage];
而不是

UIImage *image=[info valueForKey:@"UIImagePickerControllerOriginalImage"];
如果您已经这样做了,请在viewDidLoad中alloc employeePhoto

piker.allowsImageEditing = NO;
然后

如果你做了

piker.allowsImageEditing = YES;
然后使用

UIImage *image=[info objectForKey:UIImagePickerControllerEditedImage];
也许这对你有帮助。但首先检查是否在didFinishPickingMediaWithInfo中获得图像。

在使imageview=nil后,使用
-(void)imagePickerController:(UIImagePickerController*)picker…
方法

再次alloc imageview如下所示

- (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];
}
让我知道它是否工作


快乐编码

请尝试以下代码:

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

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

像这样试试:

- (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];
    });
}

是否尝试在UIImage*图像附近设置断点以查看是否返回图像,或尝试NSLog(@“image:%@”,image);。[从SuperView移除的员工照片];尝试使用此选项而不是[employeePhoto setImage:nil];
employeePhoto
是否保存在实例变量中?您在第一个代码段中创建了它,但不清楚您如何处理它。如果图像没有更改,则有2种可能性1。未调用didFinishPickingMediaWithInfo方法或2。employeePhoto为零。为什么在将图像设置为零后还要设置图像?我试着用你的建议把它去掉,但不起作用。当我使用out placeholder.png图像初始化uiimageview时,它工作正常。感谢您的评论您是否已设置UIImagePickerController的委派。。。。我尝试了不同的选择。当我在这一行推荐时/[employeePhoto setImage:[UIImage ImageName:@“placeholder.png”];然后它就可以工作了..我尝试了不同的选择。当我在这一行推荐时/[employeePhoto setImage:[UIImage ImageName:@“placeholder.png”];然后它就可以正常工作了。然后尝试将placeholder.png的名称更改为任何其他名称。
- (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];
    });
}