天天看點

UILable轉為UIImage的方法

         在做歌詞的時候,有時候歌詞顔色會發生變化 ,一般會用UIImage做剪裁,慢慢出現不同的顔色,用UIlable專為UIImage,圖檔進行繪圖操作就可以達到想要的效果了

  label轉化成image:

- (UIImage*) imageWithUIView:(UIView*) view
{
    UIGraphicsBeginImageContext(view.bounds.size);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    [view.layer renderInContext:ctx];
    UIImage* tImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return tImage;
}
           

然後進行繪圖剪裁

- (UIImage *)croppedImage:(UIImage *)image cutRect:(NSInteger)width
{
    if (image)
    {
        float min = MIN(image.size.width,image.size.height);
        CGRect rectMAX = CGRectMake(0, 0, width, min);
        CGImageRef subImageRef = CGImageCreateWithImageInRect(image.CGImage, rectMAX);
        UIGraphicsBeginImageContext(rectMAX.size);
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextDrawImage(context, CGRectMake(0, 0, width, min), subImageRef);
        UIImage *viewImage = [UIImage imageWithCGImage:subImageRef];
        UIGraphicsEndImageContext();
        CGImageRelease(subImageRef);
        return viewImage;
    }
    
    return nil;
    
}
           

這樣就可以根據時間出現緩慢的出現歌詞了。。。。。。