构建一个应用程序,让用户可以选择更改应用程序的背景。当前使用此代码从选择器中保存图像。
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
customImage = [info objectForKey:UIImagePickerControllerOriginalImage];
NSData *data = UIImagePNGRepresentation(customImage);
NSString *fetchCustomImage = @"userCustomImage.png";
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [path objectAtIndex:0];
NSString *fullPathToFile = [documentDirectory stringByAppendingPathComponent:fetchCustomImage];
[data writeToFile:fullPathToFile atomically:YES];
[self dismissViewControllerAnimated:YES completion:NULL];
[self performSelector:@selector(fetchCustomBackground)]
}
然后调用一个void来显示图像
- (void)fetchCustomBackground
{
//Fetch Background Image
NSString *fetchUserImage = @"userCustomImage.png";
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [path objectAtIndex:0];
NSString *fullPath = [documentDirectory stringByAppendingPathComponent:fetchUserImage];
NSData *data = [NSData dataWithContentsOfFile:fullPath];
[background setImage:[UIImage imageWithData:data]];
}
在 viewDidLoad
[self performSelector:@selector(fetchCustomBackground)];
目前该应用程序非常慢,我猜是因为每次加载视图时都必须获取图像,有没有办法保存它,这样您就不必每次加载视图时都调用它?