我的class foo(){
private:
void a(){
// function definition
}
}
class other_foo(){
public:
void b(foo f){
// function definition
}
}
有一个UIImageView
。
我想要的是,当点击UIImage
时,用户可以从他们的库中选择一张照片,以显示UIImage
而不是我设置的默认图片
UIImageView
编辑:如果我可以使用一堆图像制作视图,并且当单击图像时,如果有意义,则将“myimg”更改为单击的图像
编辑#2:另外,如何保存用户选择的照片,并在用户下次打开应用程序时使用它
答案 0 :(得分:0)
您需要的是使用UIImagePickerController
作为源类型实现UIImagePickerControllerSourceTypePhotoLibrary
。
stackoverflow中有大量的教程和问题/答案。
检查例如thread。它有很好的答案,参考链接到Apple的示例代码和其他有趣的教程。
编辑#2:编辑#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;
}
答案 1 :(得分:0)
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];
}
答案 2 :(得分:0)
您可以使用UIImagePickerController
从图库中获取图片并在图片视图中显示
此代码将您带到Gallery
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
中,以便您下次用户打开应用程序时可以使用它。 / p>
NSData *imageData = UIImageJPEGRepresentation(chosenImage, 1.0);
encodedString = [imageData base64EncodedStringWithOptions:0];
NSLog(@"chosenImage : %@",chosenImage);
这将为您提供图像。
要将此图片设置为ImageView
,您可以使用
UIImage *image = [UIImage imageWithData:data];
yourImageView.image = image;
希望它有所帮助。