【发布时间】:2013-04-02 04:05:58
【问题描述】:
当我的 iPhone 应用程序第一次运行时,它会要求您从 iPhone 库中选择一张图片作为整个应用程序的背景。以下是相关代码:
/*
* Responds when an image is selected (from browsing)
*/
-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
[self dismissModalViewControllerAnimated:YES];
if ([mediaType isEqualToString:(NSString *)kUTTypeImage])
{
//Not really sure what to do here
UIImage *image = [[UIImage alloc] initWithData:UIImageJPEGRepresentation([info objectForKey:UIImagePickerController], .2)];
}
}
我现在要保存这张图片(或对这张图片的引用),以便我可以在整个应用程序中使用它作为我的 UIViewController 的某些 (60%) 的背景
self.view.backgroundColor = [UIColor colorWithPatternImage:image];
最好/最有效/最简单的方法是什么。我想过将图像保存到NSUserDefaults,但这似乎不正确,因为NSUserDefaults 应该只用于小物体。我应该将其保存到磁盘吗?但是,我的应用程序在每次 segue 时都必须从磁盘读取数据会不会很慢? (如果这是正确的方法,我该怎么做?)
【问题讨论】:
-
我注意到您将答案标记为正确。无需更改,但请考虑,如果您进行子类化的唯一原因是获取该背景颜色,您可以使用外观协议同样有效地(并且没有额外的子类):[[UIView appearance] setBackgroundColor: [UIColor colorWithPatternImage:img]]; (或 appearanceWhenContainedIn 可能对您更有意义)。
-
这将适用于所有 ViewControllers 吗?
-
所有视图。您可以使用 whenContainedIn 变体来缩小范围。
标签: iphone ios objective-c uiimage uiimagepickercontroller