我有一个函数,它获取一些位图数据并从中返回一个UIImage*.它看起来像这样:
UIImage * makeAnImage()
{
unsigned char * pixels = malloc(...);
// ...
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, pixels, pixelBufferSize, NULL);
CGImageRef imageRef = CGImageCreate(..., provider, ...);
UIImage * image = [[UIImage alloc] initWithCGImage:imageRef];
return [image autorelease];
}
Run Code Online (Sandbox Code Playgroud)
任何人都可以确切地解释谁在这里拥有什么记忆?我想要正确清理,但我不确定如何安全地进行清理.文档在这些上是模糊的.如果free在创建UIImage之后我在此函数末尾的像素,然后使用UIImage,我崩溃了.如果我在创建UIImage之后释放提供者或imageRef,我没有看到崩溃,但他们显然已经完全传递了像素,所以我对释放这些中间状态感到不安.
(我知道每个CF文档我都需要在后者上调用release,因为它们来自Create函数,但是我可以在使用UIImage之前这样做吗?)大概我可以使用提供者的dealloc回调来清理像素缓冲区,还有什么?
谢谢!
我有一个EAGLView(取自Apple的例子),我可以使用以下代码成功转换为UIImage:
- (UIImage *)glToUIImage:(CGSize)size {
NSInteger backingWidth = size.width;
NSInteger backingHeight = size.height;
NSInteger myDataLength = backingWidth * backingHeight * 4;
// allocate array and read pixels into it.
GLuint *buffer = (GLuint *) malloc(myDataLength);
glReadPixels(0, 0, backingWidth, backingHeight, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
// gl renders “upside down” so swap top to bottom into new array.
for(int y = 0; y < backingHeight / 2; y++) {
for(int x = 0; x < backingWidth; x++) {
//Swap top and bottom bytes …Run Code Online (Sandbox Code Playgroud) 我正在使用UIWebView来显示HTML格式的文本.我没有加载网页,只是向UIWebView提供了一串HTML.
现在我想在屏幕上设置这个UIWebView的动画,实际上有几个(一次2-10个).UIWebView有点沉重,虽然我还没有尝试过,但我计划做最坏的事情.(我不认为这是不成熟的优化,我几乎肯定这将是一个问题)
为了解决这个问题,我想我可以将UIWebViews的内容转换为UIImages,并为它们设置动画.
所以,我的问题是:
谢谢你的任何建议
我一直在寻找很长时间,我发现了一些提示,但我无法弄清楚如何将其应用于我的问题。将不胜感激任何帮助。
试图实现的目标非常简单:
我的应用程序包括一个播放 youtube 视频的 WKWebview(带有一些叠加文本)。我添加了一个链接来截取屏幕截图并将其保存到相机胶卷中。
结果:
它在模拟器中完美运行,但在设备上我只能看到覆盖文本和背景(应该是视频)是黑色的。
这是我当前的代码(SWIFT 3):
...
// Save screenshot
UIImageWriteToSavedPhotosAlbum(captureScreen(), nil, nil, nil)
...
func captureScreen() -> UIImage {
UIGraphicsBeginImageContextWithOptions(webView!.bounds.size, webView!.isOpaque, 0)
webView!.drawHierarchy(in: webView!.bounds, afterScreenUpdates: false)
let image = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return image
}
Run Code Online (Sandbox Code Playgroud)
我也试过(结果相同)
...
UIImageWriteToSavedPhotosAlbum(captureScreen(), nil, nil, nil)
...
func captureScreen() -> UIImage {
let image = UIApplication.shared.screenShot!
return image
}
Run Code Online (Sandbox Code Playgroud)
我在网上读到我们不能使用 renderInContext: 因为没有捕获包含视频播放器的 GL 层。所以他们建议实际合并 UIImageView 和 EAGLview ( link1 , link2 ) 的内容。
这听起来不错,但我发现的大多数解决方案都是用 Objective-c 编写的或相对复杂的(link3、link4、link5 …
iphone ×3
ios ×2
cocoa-touch ×1
eaglview ×1
objective-c ×1
opengl-es ×1
quartz-2d ×1
screenshot ×1
swift ×1
uiimage ×1
uiview ×1
wkwebview ×1
xcode ×1