0

在过去的几天里,我一直在寻找如何零星地执行此操作,但未能找到任何东西,希望您能提供帮助。

目前在我的应用程序中,我有 5 个不同的背景作为项目文件的一部分,并允许用户在它们之间进行选择以设置应用程序背景,从而为应用程序提供一些自定义特性。

我想要做的是允许用户选择他们保存在他们的设备上的照片并将其用作我的应用程序的背景,但我无法找到如何做到这一点,或者可能在不知道的情况下错过了它(我来自一个主要的 C# 背景,所以仍然适应术语和学习知识语言和 IDE 带来的所有有趣的事情)。

我希望在代码中做的是:

  • 用户点击使用自己的图片
  • 应用程序打开窗口以浏览图像
  • 用户选择要使用的图像
  • 分配和保存路径(或以某种方式将图像保存在应用程序中)
  • 将 imageview 设置为该图像

感谢大家提供的任何帮助以及您提供帮助所花费的时间。作为 iPhone 5.0 的仅供参考

4

3 回答 3

2

在 nib 文件和按钮中创建一个 UIImageView 并挂钩 nib

在 ViewController.h

     @interface ViewController : UIViewController
        <UIImagePickerControllerDelegate,UINavigationControllerDelegate>
    {
       IBOutlet  UIButton *ChooseFileBtn;
    }

@property (nonatomic,retain) UIImage *userimage;
-(IBAction)getphoto:(id)sender;

在 ViewController.m 中

 -(IBAction)getphoto:(id)sender {

    UIImagePickerController * picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
        [self presentViewController:picker animated:YES completion:nil];
    }
    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
        [picker dismissViewControllerAnimated:YES completion:nil];
        userimage = [info objectForKey:UIImagePickerControllerOriginalImage];
        imageView.image=userimage;
    }
于 2013-06-20T04:52:03.917 回答
1

您可以使用 UIImagePickerController 从照片库中获取图像

-(void)ClickOnsetBG:(id)sender{
    UIImagePickerController *imgpicker = [[UIImagePickerController alloc] init];
    imgpicker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
    imgpicker.delegate=self;

    [self.navigationController presentModalViewController:imgpicker animated:YES];

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5];
    [UIView commitAnimations];
}

然后实现委托方法来获取选定的图像:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
 {
     UIImage *aImgRecord = [info objectForKey:UIImagePickerControllerOriginalImage];
     [imgpicker dismissModalViewControllerAnimated:YES];
     bgImage.image =aImgRecord;

     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
 NSUserDomainMask, YES);
     NSString *documentsDirectory = [paths objectAtIndex:0];
     NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:@"custombg.png"];
     NSData *imageData = UIImagePNGRepresentation(aImgRecord);    
     [imageData writeToFile:savedImagePath atomically:NO];
}

此图像将保存在您的应用程序的文档目录中。因此,当您重新打开您的应用程序时,您必须从此文档目录路径设置 bgimage。

于 2013-06-20T04:52:11.493 回答
0

You can do this with NSUserDefaults also

-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
{
    UIImage *newImage = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    NSData *imageData = UIImageJPEGRepresentation(newImage, 1);
    [[NSUserDefaults standardUserDefaults] setObject:imageData forKey:@"BackgroundImage"];
    [picker dismissViewControllerAnimated:YES completion:^ {
        // If you have used UIImageView
        [self.yourImageViewObject setImage:newImage];
    }];
}

Later the image could be easily shown through NSUserDefaults like

UIImage *image = [UIImage imageWithData:[[NSUserDefaults standardUserDefaults] objectForKey:@"BackgroundImage"]];
于 2013-06-20T07:42:22.657 回答