2012-01-09 62 views
2

我知道如何呈現一個正常的UIView位圖圖像:渲染完整的UIImageView位圖圖像

UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, 0.0); 
[view.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage *bitmapImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

的問題是,如果viewresizableImageWithCapInsets:返回了具有拉伸可調整大小的圖像UIImageViewbitmapImage以這種方式得到的是原始圖像 - 未拉伸的圖像 - 而不是真正顯示的拉伸圖像。我可以很容易地看出,由bitmapImageview之間的大小差異。所以我的問題是如何渲染由UIImageView顯示的完整內容,其圖像是一個拉伸的可調整大小的圖像?

+0

你能澄清這個問題嗎? – 2012-01-16 21:12:38

+0

當然。哪一部分對你不清楚? – an0 2012-01-17 11:06:48

回答

0

我是個白癡!代碼完美地工作。我只是把別的東西搞亂了。愚蠢的是我把代碼放在我的UIView類別的一個方法中,但我將方法命名爲image。它完全適用於一般UIView S,但作爲UIImageView有自己的image方法和UIImageViewUIView一個子類,UIImageViewimage方法,當我試圖讓我的位圖圖像調用。

1

所以,我想這段代碼:

UIEdgeInsets edgeInsets = UIEdgeInsetsMake(-20, -20, -20, -20); 
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)]; 
imageView.image = [[UIImage imageNamed:@"number1.png"] resizableImageWithCapInsets:edgeInsets]; 
[self.view addSubview:imageView]; 
UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, NO, 0.0); 
[imageView.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage *bmImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

NSArray *documentsPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDir = [documentsPaths objectAtIndex:0]; 
[UIImagePNGRepresentation(bmImage) writeToFile:[documentsDir stringByAppendingString:@"num1.png"] atomically:YES]; 

,並得到這個:

enter image description here

,並從這樣的:

enter image description here

所以它看起來像代碼的工作原理應該如此。