允许用户将图像更改为图库中的图像
Allow user to change image to one from their gallery
我有一个 UIImageView
有一个 UIImage
。
我想要的是,当单击 UIImage
时,用户可以 select 从他们的图库中显示照片 UIImageView
而不是我设置的默认图像
imageView = [[UIImageView alloc] init];
UIImage *myimg = [UIImage imageNamed:@"ProfilePic.png"];
imageView.image=myimg;
imageView.frame = CGRectMake(100,0, 80, 50); // pass your frame here
[setsView addSubview:imageView];
imageView.layer.backgroundColor=[[UIColor clearColor] CGColor];
imageView.layer.cornerRadius=20;
imageView.layer.borderWidth=2.0;
imageView.layer.masksToBounds = YES;
imageView.layer.borderColor=[[UIColor redColor] CGColor];
编辑:如果我可以用一堆图像创建一个视图,当单击图像时它会将 "myimg" 更改为单击的图像(如果这有意义的话)
编辑 #2:另外,您将如何保存用户选择的照片并在用户下次打开应用程序时使用它
您需要以 UIImagePickerControllerSourceTypePhotoLibrary
作为源类型来实现 UIImagePickerController
。
Whosebug 中有大量教程和 questions/answers。
例如检查这个 thread。它有很好的答案,带有指向 Apple 示例代码和其他有趣教程的参考链接。
编辑#2:
好吧,这也有很多方法可以完成,这取决于您的用例。但是快速简单的方法是使用NSUserDefaults
。
例如:
这是为了保存图像,
NSData * data = UIImagePNGRepresentation(yourUImage);
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:data forKey:@"userImage"];
[defaults synchronize];
并获取图像
- (void)viewDidLoad
{
[super viewDidLoad];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *data = [defaults objectForKey:@"userImage"];
UIImage *image = [UIImage imageWithData:data];
yourImageView.image = image;
}
UITapGestureRecognizer *tapGesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imageAction:)];
[self.imgPhotoStrand setUserInteractionEnabled:YES];
[self.imgPhotoStrand addGestureRecognizer:tapGesture];
-(void)imageAction:(UITapGestureRecognizer *)sender{
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Edit Photo"
delegate:self
cancelButtonTitle:@"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:@"Camera", @"Select from Library", nil];
actionSheet.actionSheetStyle = UIActionSheetStyleAutomatic;
[actionSheet showInView:self.view];
}
#pragma mark - UIActionSheetDelegate -
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
switch(buttonIndex)
{
case 0:
{
if ([UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera]) {
if (IS_IPAD) {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[self camera];
}];
}else if(IS_IPHONE){
[self camera];
}
}
else
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:ALERTTITLE message:@"No Camara Found" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alertView show];
}
}
break;
case 1:
{
if (IS_IPAD) {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[self library];
}];
}else if(IS_IPHONE){
[self library];
}
}
default:
// Do Nothing.........
break;
}
}
- (void)camera {
if(![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
return;
}
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
//Permetto la modifica delle foto
picker.allowsEditing = YES;
//Imposto il delegato
[picker setDelegate:self];
[self presentViewController:picker animated:YES completion:nil];
}
- (void)library {
//Inizializzo la classe per la gestione della libreria immagine
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
//Permetto la modifica delle foto
picker.allowsEditing = YES;
//Imposto il delegato
[picker setDelegate:self];
[self presentViewController:picker animated:YES completion:nil];
}
#pragma mark UIImagePickerController Delegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
[self dismissViewControllerAnimated:YES completion:^{
UIImage *image = [info valueForKey:UIImagePickerControllerEditedImage];
self.imgPhotoStrand.image =image;
}];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[self dismissViewControllerAnimated:YES completion:nil];
}
您可以使用 UIImagePickerController
从图库中获取图像并在图像视图中显示它
此代码会将您带到图库
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];
然后这里是 UIImagePickerController
的委托方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
chosenImage = info[UIImagePickerControllerEditedImage];
yourImageView.image = chosenImage;
[picker dismissViewControllerAnimated:YES completion:NULL];
}
这对我有用。
希望对你有帮助。
1]您可以为每张图片使用自定义按钮,这样当您点击图片按钮时,目标就会被调用,您可以使用点击的图片更改您的图片。
2]为了存储图像,您可以使用 NSUserDefaults
这样即使应用程序关闭,您的图像也会存储在 NSUserDefaults
中以便下次用户打开应用程序时可以使用它.
NSData *imageData = UIImageJPEGRepresentation(chosenImage, 1.0);
encodedString = [imageData base64EncodedStringWithOptions:0];
NSLog(@"chosenImage : %@",chosenImage);
这将为您提供图像。
要将此图像设置为 ImageView
,您可以使用
UIImage *image = [UIImage imageWithData:data];
yourImageView.image = image;
希望对您有所帮助。
我有一个 UIImageView
有一个 UIImage
。
我想要的是,当单击 UIImage
时,用户可以 select 从他们的图库中显示照片 UIImageView
而不是我设置的默认图像
imageView = [[UIImageView alloc] init];
UIImage *myimg = [UIImage imageNamed:@"ProfilePic.png"];
imageView.image=myimg;
imageView.frame = CGRectMake(100,0, 80, 50); // pass your frame here
[setsView addSubview:imageView];
imageView.layer.backgroundColor=[[UIColor clearColor] CGColor];
imageView.layer.cornerRadius=20;
imageView.layer.borderWidth=2.0;
imageView.layer.masksToBounds = YES;
imageView.layer.borderColor=[[UIColor redColor] CGColor];
编辑:如果我可以用一堆图像创建一个视图,当单击图像时它会将 "myimg" 更改为单击的图像(如果这有意义的话)
编辑 #2:另外,您将如何保存用户选择的照片并在用户下次打开应用程序时使用它
您需要以 UIImagePickerControllerSourceTypePhotoLibrary
作为源类型来实现 UIImagePickerController
。
Whosebug 中有大量教程和 questions/answers。
例如检查这个 thread。它有很好的答案,带有指向 Apple 示例代码和其他有趣教程的参考链接。
编辑#2:
好吧,这也有很多方法可以完成,这取决于您的用例。但是快速简单的方法是使用NSUserDefaults
。
例如: 这是为了保存图像,
NSData * data = UIImagePNGRepresentation(yourUImage);
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:data forKey:@"userImage"];
[defaults synchronize];
并获取图像
- (void)viewDidLoad
{
[super viewDidLoad];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *data = [defaults objectForKey:@"userImage"];
UIImage *image = [UIImage imageWithData:data];
yourImageView.image = image;
}
UITapGestureRecognizer *tapGesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imageAction:)];
[self.imgPhotoStrand setUserInteractionEnabled:YES];
[self.imgPhotoStrand addGestureRecognizer:tapGesture];
-(void)imageAction:(UITapGestureRecognizer *)sender{
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Edit Photo"
delegate:self
cancelButtonTitle:@"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:@"Camera", @"Select from Library", nil];
actionSheet.actionSheetStyle = UIActionSheetStyleAutomatic;
[actionSheet showInView:self.view];
}
#pragma mark - UIActionSheetDelegate -
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
switch(buttonIndex)
{
case 0:
{
if ([UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera]) {
if (IS_IPAD) {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[self camera];
}];
}else if(IS_IPHONE){
[self camera];
}
}
else
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:ALERTTITLE message:@"No Camara Found" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alertView show];
}
}
break;
case 1:
{
if (IS_IPAD) {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[self library];
}];
}else if(IS_IPHONE){
[self library];
}
}
default:
// Do Nothing.........
break;
}
}
- (void)camera {
if(![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
return;
}
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
//Permetto la modifica delle foto
picker.allowsEditing = YES;
//Imposto il delegato
[picker setDelegate:self];
[self presentViewController:picker animated:YES completion:nil];
}
- (void)library {
//Inizializzo la classe per la gestione della libreria immagine
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
//Permetto la modifica delle foto
picker.allowsEditing = YES;
//Imposto il delegato
[picker setDelegate:self];
[self presentViewController:picker animated:YES completion:nil];
}
#pragma mark UIImagePickerController Delegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
[self dismissViewControllerAnimated:YES completion:^{
UIImage *image = [info valueForKey:UIImagePickerControllerEditedImage];
self.imgPhotoStrand.image =image;
}];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[self dismissViewControllerAnimated:YES completion:nil];
}
您可以使用 UIImagePickerController
从图库中获取图像并在图像视图中显示它
此代码会将您带到图库
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];
然后这里是 UIImagePickerController
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
chosenImage = info[UIImagePickerControllerEditedImage];
yourImageView.image = chosenImage;
[picker dismissViewControllerAnimated:YES completion:NULL];
}
这对我有用。 希望对你有帮助。
1]您可以为每张图片使用自定义按钮,这样当您点击图片按钮时,目标就会被调用,您可以使用点击的图片更改您的图片。
2]为了存储图像,您可以使用 NSUserDefaults
这样即使应用程序关闭,您的图像也会存储在 NSUserDefaults
中以便下次用户打开应用程序时可以使用它.
NSData *imageData = UIImageJPEGRepresentation(chosenImage, 1.0);
encodedString = [imageData base64EncodedStringWithOptions:0];
NSLog(@"chosenImage : %@",chosenImage);
这将为您提供图像。
要将此图像设置为 ImageView
,您可以使用
UIImage *image = [UIImage imageWithData:data];
yourImageView.image = image;
希望对您有所帮助。