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

时间:2013-06-20 04:35:16

标签: ios objective-c uiimageview user-input

过去几天我一直在四处寻找如何零星地做这件事并且找不到任何东西,并希望你能提供帮助。

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

我要做的是允许用户选择他们已保存在他们设备上的照片并将其用作我的应用的背景但无法知道如何操作或者可能在不知情的情况下错过了我来自一个主要的C#背景,所以仍然适应了termanology以及学习知识和IDE所带来的所有乐趣。

我想在代码中做的是:

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

感谢大家为您提供的任何帮助以及您提供的时间。作为iPhone 5.0的FYI Im建筑

3 个答案:

答案 0 :(得分:2)

在nib文件和按钮中创建UIImageView并挂钩笔尖

在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;
    }

答案 1 :(得分:1)

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

-(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。

答案 2 :(得分:0)

您也可以使用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];
    }];
}

稍后可以通过NSUserDefaults轻松显示图像,如

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