Ios 如何允许用户在我的应用程序中将其保存的照片用作背景?

Ios 如何允许用户在我的应用程序中将其保存的照片用作背景?,ios,objective-c,uiimageview,user-input,Ios,Objective C,Uiimageview,User Input,在过去的几天里,我一直在四处寻找如何做到这一点,但一直没有找到任何东西,希望你能提供帮助 目前在我的应用程序中,作为项目文件的一部分,我有5个不同的背景,允许用户在它们之间进行选择,以设置应用程序背景,为应用程序提供一些定制 我想做的是允许用户选择他们保存在他们设备上的照片,并将其用作我的应用程序的背景,但在不知道我来自原始C背景的情况下,我无法找到如何做,或者可能错过了它,所以仍然适应术语和学习know lang所带来的所有有趣的事情和IDE 我希望在代码中做的是: 用户单击以使用自己的图像

在过去的几天里,我一直在四处寻找如何做到这一点,但一直没有找到任何东西,希望你能提供帮助

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

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

我希望在代码中做的是:

用户单击以使用自己的图像 应用程序打开窗口浏览图像 用户选择要使用的图像 在应用程序中指定并保存路径或保存图像 将imageview设置为该图像 感谢大家为您提供的任何帮助以及您花费的时间。作为供参考的iPhone5.0 Im构建

在nib文件中创建UIImageView并按下按钮,然后钩住nib

在ViewController.h中

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

@property (nonatomic,retain) UIImage *userimage;
-(IBAction)getphoto:(id)sender;
在nib文件中创建UIImageView,并单击按钮并钩住nib

在ViewController.h中

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

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

您可以使用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。

您可以使用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。

您也可以使用NSUserDefaults执行此操作

-(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];
    }];
}
之后,图像可以很容易地通过默认值显示,如

UIImage *image = [UIImage imageWithData:[[NSUserDefaults standardUserDefaults] objectForKey:@"BackgroundImage"]];

您也可以使用NSUserDefaults执行此操作

-(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];
    }];
}
之后,图像可以很容易地通过默认值显示,如

UIImage *image = [UIImage imageWithData:[[NSUserDefaults standardUserDefaults] objectForKey:@"BackgroundImage"]];

使用UIImagePickerController。使用UIImagePickerController。您不应该使用字符串文字@UIImagePickerController原始图像。相反,使用提供的常量:userimage=[info-objectForKey:UIImagePickerControllerOriginalImage];非常感谢你。比我想的要容易得多。甚至不必为浏览创建一个我认为必须创建的新视图:梅尼感谢你的帮助,感谢你和所有其他人花了这么多时间才给出你的答案。非常感谢,您不应该使用字符串literal@UIImagePickerControllerOriginalImage。相反,使用提供的常量:userimage=[info-objectForKey:UIImagePickerControllerOriginalImage];非常感谢你。比我想的要容易得多。甚至不必为浏览创建一个我认为必须创建的新视图:梅尼感谢你的帮助,感谢你和所有其他人花了这么多时间才给出你的答案。非常感谢,您不应该使用字符串literal@UIImagePickerControllerOriginalImage。相反,使用提供的常量:UIImage*aImgRecord=[info objectForKey:UIImagePickerControllerOriginalImage];UIView beginAnimations和UIView Committeanimations的要点是什么?您不应该使用字符串literal@uiImagePickerController原始图像。相反,使用提供的常量:UIImage*aImgRecord=[info objectForKey:UIImagePickerControllerOriginalImage];UIView beginAnimations和UIView Committeanimations的观点是什么?