简体   繁体   English

更改UIImage(而不是UIImageView)的透明度

[英]Change transparency for UIImage (not UIImageView)

I am using AVAssetWriterInputPixelBufferAdaptor to create a video from an array of UIImage's, and i want to change the transition from one image to another in different forms of animations(much like a slide show ... actually exactly like a slideshow). 我正在使用AVAssetWriterInputPixelBufferAdaptor从一组UIImage创建视频,并且我想以不同形式的动画(很像幻灯演示……实际上就像幻灯演示)将一个图像转换为另一个图像。

One of these transitional styles is fade out. 这些过渡样式之一是淡出。 since im writing UIImages like so .. 由于即时通讯这样写UIImages ..

    UIImage *image = imageView.image;
    UIImage *resizedImage = [ICVideoViewController imageWithImage:image scaledToSize:CGSizeMake(640, 480)];
    CGImageRef img = [resizedImage CGImage];
    CVPixelBufferRef buffer = [ICVideoViewController pixelBufferFromCGImage:img size:CGSizeMake(640, 480)];

    [adaptor appendPixelBuffer:buffer withPresentationTime:CMTimeMake(i, 1)];

I would like to know if it is possible (without having to manually manipulate the pixels) to achieve this .. ? 我想知道是否有可能(无需手动操作像素)来实现这一目标。

I dont know how you are creating image in imagawithimage function of your view controller. 我不知道您如何在视图控制器的imagawithimage函数中创建图像。

If you are using CGContext to scale image then use CGContextSetAlpha() before CGContextDrawImage() . 如果您正在使用CGContext上缩放图像,然后使用CGContextSetAlpha()之前CGContextDrawImage()

If not you can do one more thing, no doubt you will need to code few more lines for that but this can be one way : 如果不能,那么您可以再做一件事,毫无疑问,您将需要为此编写几行代码,但这可以是一种方法:

CGSize size = [self size];
int width = size.width;
int height = size.height;

// the pixels will be painted to this array
uint32_t *pixels = (uint32_t *) malloc(width * height * sizeof(uint32_t));

// clear the pixels so any transparency is preserved
memset(pixels, 0, width * height * sizeof(uint32_t));

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

// create a context with RGBA pixels
CGContextRef context = CGBitmapContextCreate(pixels, width, height, 8, width * sizeof(uint32_t), colorSpace, 
                                             kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedLast);

// paint the bitmap to our context which will fill in the pixels array
CGContextSetAlpha(context, 0.5);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), [self CGImage]);
CGImageRef image = CGBitmapContextCreateImage(context);

CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
free(pixels);

How about blending the two images you're transitioning? 如何融合要转换的两个图像?

See the following thread: blend two uiimages based on alpha/transparency of top image 请参见以下线程: 根据顶部图像的Alpha /透明度来混合两个uiimages

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM