UIImageViewでこれを実行できることは知っていますが、UIImageで実行できますか?UIImageViewのアニメーション画像の配列プロパティを同じ画像の配列にしたいのですが、不透明度が異なります。考え?
回答:
私はこれを行う必要がありましたが、スティーブンの解決策は遅いだろうと思いました。これはうまくいけばグラフィックスハードウェアを使用する必要があります。UIImageにカテゴリを作成します。
- (UIImage *)imageByApplyingAlpha:(CGFloat) alpha {
    UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0f);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGRect area = CGRectMake(0, 0, self.size.width, self.size.height);
    CGContextScaleCTM(ctx, 1, -1);
    CGContextTranslateCTM(ctx, 0, -area.size.height);
    CGContextSetBlendMode(ctx, kCGBlendModeMultiply);
    CGContextSetAlpha(ctx, alpha);
    CGContextDrawImage(ctx, area, self.CGImage);
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}
に表示されるビューの不透明度を設定します。
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageWithName:@"SomeName.png"]];
imageView.alpha = 0.5; //Alpha runs from 0.0 to 1.0
これをアニメーションで使用します。アニメーションのアルファを一定期間変更できます。
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0];
//Set alpha
[UIView commitAnimations];
UIButton、私は必要なまさに、。
                    Alexey Ishkovの回答に基づいていますが、Swiftで
UIImageクラスの拡張機能を使用しました。
スウィフト2:
UIImage拡張機能:
extension UIImage {
    func imageWithAlpha(alpha: CGFloat) -> UIImage {
        UIGraphicsBeginImageContextWithOptions(size, false, scale)
        drawAtPoint(CGPointZero, blendMode: .Normal, alpha: alpha)
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return newImage
    }
}
使用するには:
let image = UIImage(named: "my_image")
let transparentImage = image.imageWithAlpha(0.5)
スウィフト3/4/5:
この実装はオプションのUIImageを返すことに注意してください。これは、Swift3でUIGraphicsGetImageFromCurrentImageContextオプションが返されるようになったためです。コンテキストがnilの場合、またはで作成されていない場合、この値はnilになる可能性がありますUIGraphicsBeginImageContext。
UIImage拡張機能:
extension UIImage {
    func image(alpha: CGFloat) -> UIImage? {
        UIGraphicsBeginImageContextWithOptions(size, false, scale)
        draw(at: .zero, blendMode: .normal, alpha: alpha)
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return newImage
    }
}
使用するには:
let image = UIImage(named: "my_image")
let transparentImage = image?.image(alpha: 0.5)
Swift 3バージョンが好きです。
                    はるかに簡単な解決策があります:
- (UIImage *)tranlucentWithAlpha:(CGFloat)alpha
{
    UIGraphicsBeginImageContextWithOptions(self.size, NO, self.scale);
    [self drawAtPoint:CGPointZero blendMode:kCGBlendModeNormal alpha:alpha];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}
これはかなり遅いと思いますが、私はこのようなものが必要だったので、これを行うために迅速で汚い方法を作り上げました。
+ (UIImage *) image:(UIImage *)image withAlpha:(CGFloat)alpha{
    // Create a pixel buffer in an easy to use format
    CGImageRef imageRef = [image CGImage];
    NSUInteger width = CGImageGetWidth(imageRef);
    NSUInteger height = CGImageGetHeight(imageRef);
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    UInt8 * m_PixelBuf = malloc(sizeof(UInt8) * height * width * 4);
    NSUInteger bytesPerPixel = 4;
    NSUInteger bytesPerRow = bytesPerPixel * width;
    NSUInteger bitsPerComponent = 8;
    CGContextRef context = CGBitmapContextCreate(m_PixelBuf, width, height,
                                                 bitsPerComponent, bytesPerRow, colorSpace,
                                                 kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
    CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
    CGContextRelease(context);
    //alter the alpha
    int length = height * width * 4;
    for (int i=0; i<length; i+=4)
    {
        m_PixelBuf[i+3] =  255*alpha;
    }
    //create a new image
    CGContextRef ctx = CGBitmapContextCreate(m_PixelBuf, width, height,
                                                 bitsPerComponent, bytesPerRow, colorSpace,
                                                 kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
    CGImageRef newImgRef = CGBitmapContextCreateImage(ctx);  
    CGColorSpaceRelease(colorSpace);
    CGContextRelease(ctx);  
    free(m_PixelBuf);
    UIImage *finalImage = [UIImage imageWithCGImage:newImgRef];
    CGImageRelease(newImgRef);  
    return finalImage;
}
setImage:withAlpha:
                    image:withAlpha:ですか?
                    Xamarinユーザーからのおかげでねえねえ!:)ここでそれはc#に翻訳されます
//***************************************************************************
public static class ImageExtensions
//***************************************************************************
{
    //-------------------------------------------------------------
    public static UIImage WithAlpha(this UIImage image, float alpha)  
    //-------------------------------------------------------------
        {
        UIGraphics.BeginImageContextWithOptions(image.Size,false,image.CurrentScale);
        image.Draw(CGPoint.Empty, CGBlendMode.Normal, alpha);
        var newImage = UIGraphics.GetImageFromCurrentImageContext();
        UIGraphics.EndImageContext();
        return newImage;
        }
}
使用例:
var MySupaImage = UIImage.FromBundle("opaquestuff.png").WithAlpha(0.15f);
Metalレンダリングを試していて、最初の応答でimageByApplyingAlphaによって生成されたCGImageを抽出している場合、予想よりも大きいMetalレンダリングになってしまう可能性があります。Metalの実験中に、imageByApplyingAlphaの1行のコードを変更したい場合があります。
    UIGraphicsBeginImageContextWithOptions (self.size, NO, 1.0f);
//  UIGraphicsBeginImageContextWithOptions (self.size, NO, 0.0f);
iPhone 11 Pro Maxのように倍率3.0のデバイスを使用している場合、上記の倍率0.0を使用すると、予想の3倍のCGImageが得られます。倍率を1.0に変更すると、スケーリングを回避できます。
うまくいけば、この返信は初心者の多くの悪化を救うでしょう。