从iPhone上的其他两个UIImages创建一个UIImage

14 iphone uiimage

我正试图在iPhone上写一个动画,没有太大成功,崩溃似乎没什么用.

我想做的事情看似简单,创建一个UIImage,并将另一个UIImage的一部分绘制到其中,我对上下文和图层和东西有点混淆.

有人可以用示例代码解释如何编写类似的东西(高效)吗?

dpj*_*nes 45

为了记录,事实证明这是相当简单的 - 你需要知道的一切都在下面的例子中:

+ (UIImage*) addStarToThumb:(UIImage*)thumb
{
   CGSize size = CGSizeMake(50, 50);
   UIGraphicsBeginImageContext(size);

   CGPoint thumbPoint = CGPointMake(0, 25 - thumb.size.height / 2);
   [thumb drawAtPoint:thumbPoint];

   UIImage* starred = [UIImage imageNamed:@"starred.png"];

   CGPoint starredPoint = CGPointMake(0, 0);
   [starred drawAtPoint:starredPoint];

   UIImage* result = UIGraphicsGetImageFromCurrentImageContext();
   UIGraphicsEndImageContext();

   return result;
}
Run Code Online (Sandbox Code Playgroud)

  • 对于视网膜模式,使用`UIGraphicsBeginImageContextWithOptions(size,NO,0);` (3认同)

She*_*ami 9

我只是想通过dpjanes添加关于上面答案的评论,因为它是一个很好的答案,但在iPhone 4(具有高分辨率视网膜显示)上会看起来很块,因为"UIGraphicsGetImageFromCurrentImageContext()"不能以完整的分辨率呈现iPhone 4.

请改用"...... WithOptions()".但由于在iOS 4.0之前无法使用WithOptions,因此您可能会将其弱化(此处讨论),然后使用以下代码仅在受支持的情况下使用hires版本:

if (UIGraphicsBeginImageContextWithOptions != NULL) {
    UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
}
else {
    UIGraphicsBeginImageContext();
}
Run Code Online (Sandbox Code Playgroud)