我希望用户能够从他们的照片中选择图片并将其设置为主视图的背景。如何设置图像作为UIView的背景,但正确缩放?
这是目前我在做什么:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissModalViewControllerAnimated:YES];
UIImage *testimg = [info objectForKey:UIImagePickerControllerOriginalImage];
NSArray * sysPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
filePath = [[NSString alloc] initWithFormat: @"%@/%@", [sysPaths objectAtIndex: 0], @"testimg.png"];
if ([UIImagePNGRepresentation (testimg) writeToFile:filePath atomically:YES]) ;
self.backgroundColor= [[UIColor alloc] initWithPatternImage:[[UIImage alloc] initWithContentsOfFile: filePath]];
}
此方法
除了能在速度上有点迟缓,这个伟大的工程!但有一个问题。当选择图像时,图像不能正确缩放。我将如何解决这个问题?
由于提前,
-David
此外,你已经有了图像。设备上的文件I/O非常慢,因此只需使用已获取的图像,而不是读取刚写入的图像。您也可以考虑将它写在另一个线程上,以便该方法可以立即返回。你这样做的方式,你必须等待图像被压缩,写入,然后再次读取,然后再解压缩,所以自然会感觉有点呆滞:) – 2010-06-23 02:45:51