在CALayer上添加子图层

时间:2014-03-13 07:30:57

标签: ios objective-c calayer vuforia

我一直在使用Vuforia SDK进行开发,我希望将视频放在一个图层上。 当增强图像识别其校准方法以显示targetOverlayView(UIView)图层时。

 [booksDelegate setOverlayLayer:self.targetOverlayView.layer];

虽然setOverlayLayer的方法是这样的:

- (void)setOverlayLayer:(CALayer *)overlayLayer {

UIImage* image = nil;

UIGraphicsBeginImageContext(overlayLayer.frame.size);
{
    [overlayLayer renderInContext: UIGraphicsGetCurrentContext()];
    image = UIGraphicsGetImageFromCurrentImageContext();
}
UIGraphicsEndImageContext();

// Get the inner CGImage from the UIImage wrapper
CGImageRef cgImage = image.CGImage;

// Get the image size
NSInteger width = CGImageGetWidth(cgImage);
NSInteger height = CGImageGetHeight(cgImage);

// Record the number of channels
NSInteger channels = CGImageGetBitsPerPixel(cgImage)/CGImageGetBitsPerComponent(cgImage);

// Generate a CFData object from the CGImage object (a CFData object represents an area of memory)
CFDataRef imageData = CGDataProviderCopyData(CGImageGetDataProvider(cgImage));

unsigned char* pngData = new unsigned char[width * height * channels];
const int rowSize = width * channels;
const unsigned char* pixels = (unsigned char*)CFDataGetBytePtr(imageData);

// Copy the row data from bottom to top
for (int i = 0; i < height; ++i) {
    memcpy(pngData + rowSize * i, pixels + rowSize * (height - 1 - i), width * channels);
}

glClearColor(0.0f, 0.0f, 0.0f, QCAR::requiresAlpha() ? 0.0f : 1.0f);

if (!trackingTIDSet) {
    glGenTextures(1, &tID);
}

glBindTexture(GL_TEXTURE_2D, tID);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, (GLvoid*)pngData);

trackingTIDSet = YES;
trackingTextureAvailable = YES;

delete[] pngData;
CFRelease(imageData);

renderState = RS_OVERLAY;

}

它会显示一个空白屏幕,即使我更改了targetOverlayView的背景颜色,它仍然只有黑屏。 targetOverlayView.m:

self.layer.backgroundColor = [UIColor orangeColor].CGColor;

这就是我想将avplayer图层放在图层上,但仍然没有运气,它会空白。

AVURLAsset *avasset = [[AVURLAsset alloc] initWithURL:URL1 options:nil];

AVPlayerItem *item = [[AVPlayerItem alloc] initWithAsset:avasset];
player = [[[AVPlayer alloc] initWithPlayerItem:item]retain];

playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
CGSize size = self.layer.bounds.size;
float x = size.width/2.0-187.0;
float y = size.height/2.0 - 125.0;

playerLayer.frame = CGRectMake(x, y, 374, 250);
[self.layer insertSublayer:playerLayer atIndex:0];

[player play]

如何通过图层呈现视频?仅供参考,播放视频是因为我可以听到声音。

谢谢..

0 个答案:

没有答案