Create a UIImage from two other UIImages on the iPhone

后端 未结 3 1499
一生所求
一生所求 2020-12-07 16:03

I\'m trying to write an animation on the iPhone, without much success, getting crashes and nothing seems to work.

What I wanna do appears simple, create a UIImage, a

相关标签:
3条回答
  • 2020-12-07 16:30

    I just want to add a comment about the answer above by dpjanes, because it is a good answer but will look blocky on iPhone 4 (with high resolution retina display), since "UIGraphicsGetImageFromCurrentImageContext()" does not render at the full resolution of an iPhone 4.

    Use "...WithOptions()" instead. But since WithOptions is not available until iOS 4.0, you could weak link it (discussed here) then use the following code to only use the hires version if it is supported:

    if (UIGraphicsBeginImageContextWithOptions != NULL) {
        UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
    }
    else {
        UIGraphicsBeginImageContext();
    }
    
    0 讨论(0)
  • 2020-12-07 16:38

    Here is an example to merge two images that are the same size into one. I don't know if this is the best and don't know if this kind of code is posted somewhere else. Here is my two cents.

    + (UIImage *)mergeBackImage:(UIImage *)backImage withFrontImage:(UIImage *)frontImage
    {
    
        UIImage *newImage;
    
        CGRect rect = CGRectMake(0, 0, backImage.size.width, backImage.size.height);
    
        // Begin context
        UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
    
        // draw images
        [backImage drawInRect:rect];
        [frontImage drawInRect:rect];
    
        // grab context
        newImage = UIGraphicsGetImageFromCurrentImageContext();
    
        // end context
        UIGraphicsEndImageContext();
    
        return newImage;
    }
    

    Hope this helps.

    0 讨论(0)
  • 2020-12-07 16:56

    For the record, this turns out to be fairly straightforward - everything you need to know is somewhere in the example below:

    + (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;
    }
    
    0 讨论(0)
提交回复
热议问题