2013-04-26 34 views
1

Iam开发一个应用程序。我想将uiview背景设置为image.That图像大小为1.6 mb.I后面是下面的代码。使用图像快速加载UIview背景

UIGraphicsBeginImageContext(self.view.frame.size); 
[[UIImage imageNamed:@"image.png"] drawInRect:self.view.bounds]; 
UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
self.view.backgroundColor = [UIColor colorWithPatternImage:image]; 

但相比于小规模image.So需要更多的时间,请帮助我如何也载入图片的大小为3MB像图像的尺寸小。

+1

您可以使用该工具ImageOptim – Felix 2013-04-26 07:47:29

+0

只是我在建settings.Then改变了CompressPNG文件NO也将需要更多的时间来加载优化图像尺寸。 – venkat 2013-04-26 07:55:07

回答

1

你可以做绘图操作在后台排队,所以它不会冻结UI:

CGRect imgRect = self.view.bounds; 
dispatch_async(dispatch_get_global_queue(0, 0), ^{ 
    UIGraphicsBeginImageContext(imgRect.size); 
    [[UIImage imageNamed:@"image.png"] drawInRect:imgRect]; 
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    UIColor* bgcolor = [UIColor colorWithPatternImage:image]; 
    dispatch_async(dispatch_get_main_queue(), ^{ 
     self.view.backgroundColor = bgcolor; 
    }); 
}); 

而且通过使用工具ImageOptim优化图像。

+0

优化图像意味着我能做什么? – venkat 2013-04-26 09:06:13

+0

ImageOptim优化图像,使其占用更少的磁盘空间并加载速度更快。只需将图像文件拖放到窗口即可。 – Felix 2013-04-26 09:12:03

+0

但我从服务器获取图像。 – venkat 2013-04-26 09:15:49

0

其最好如果你resizeimage然后加入Application bundle执行更快。

在这里,我觉得有no need得到image首先为resizingUIView小号pattern imagebackground

您可以solve memory issue这样的:

NSString *imgfile = [[NSBundle mainBundle] pathForResource:@"image" ofType:@"png"]; 
UIImage *currentImage = [UIImage imageWithContentsOfFile:imgfile]; 
self.view.backgroundColor = [UIColor colorWithPatternImage:currentImage]; 

参考ios-imagenamed-vs-imagewithcontentsoffile链接。

编辑:使用@ phix23答案平稳UI