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