Getting an image representation of the camera prev

2020-05-24 06:49发布

When I use the standard [view.layer renderInContext:UIGraphicsGetCurrentContext()]; method for turning views into images on the iPhone camera modal view, all I get is the controls with black where the camera preview was.

Anyone have a solution for this? Speed is not an issue.

标签: iphone
4条回答
家丑人穷心不美
2楼-- · 2020-05-24 07:37

This isn't possible unfortunately. For performance the iPhone uses some form of direct rendering to draw the camera preview directly onto the screen instead of as part of a UIView surface. As such when you "capture" the camera view you will only get the UIView elements, not the preview image.

(FWIW this is similar to the reasons why its difficult to screengrab some movie software on Windows/Mac)

查看更多
冷血范
3楼-- · 2020-05-24 07:39

You can do this with the new AVFoundation stuff in iOS 4.0.

You should be able to call UIGetScreenImage() (returns a UIImage) to get a current capture of the whole screen, including the preview. That's how all the barcode apps worked before. But supposedly Apple is disallowing that now and only allow the AVFoundation technique - which only works under 4.0.

The whole reason there's even an issue is because UIGetScreenImage() is not part of the documented API, but Apple made a specific exception for using it. It's not like they are pulling current apps, but they are not allowing new submissions (or updates) that use the older technique.

There is some lobbying on behalf on a number of people to convince Apple to let app developers use the old technique for iOS 3.x only, so send an email to developer relations if you want to use it.

查看更多
Bombasti
4楼-- · 2020-05-24 07:53

Not possible from a documented api, but possible. Find the "PLCameraView" view in the camera's subviews, then call

CGImageRef img = (CGImageRef)[foundCameraView imageRef];

This will return a reference to the image that camera holds.

查看更多
霸刀☆藐视天下
5楼-- · 2020-05-24 07:55
登录 后发表回答