iphone - 如何从 uiimageview op uibutton 中删除图像

标签 iphone ios

我用 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 委托(delegate)中

- (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 而不是 UIImageViewUIImageViewUIButton 声明为 @property(nonatomic, retain) 并且我使用的是 xcode 4.6.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];

}

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

关于iphone - 如何从 uiimageview op uibutton 中删除图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17183005/

相关文章:

ios - 背景图片在 iPad 和 iPhone 上移动

iphone - Xcode 4.2 上的 ios SDK 4.3?

iphone - UITableView 分组显示每个部分的新信息

iphone - 如何更改按钮上的图像集

android - 移动 SDK 中的 oauth 2 实现

ios - 如何判断 malloc 是否在 iOS 上 swift 失败?

ios - xcode 4.2 Storyboard标识符

iphone - 我在其中定义变量(15到20个变量)..Appdelegate文件或.PCH文件或在外部定义它们

iphone - 返回强制转换为ARC下CFStringRef的NSString * -如何摆脱分析器警告?

ios - 没有这样的模块 FBSDKCoreKit 错误