亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在視網膜顯示質量不變的情況下捕捉UIView到UI圖像

如何在視網膜顯示質量不變的情況下捕捉UIView到UI圖像

慕俠2389804 2019-06-14 17:16:31
如何在視網膜顯示質量不變的情況下捕捉UIView到UI圖像我的代碼適用于正常設備,但在視網膜設備上創建模糊圖像。有人知道解決我的問題嗎?+ (UIImage *) imageWithView:(UIView *)view{     UIGraphicsBeginImageContext(view.bounds.size);     [view.layer renderInContext:UIGraphicsGetCurrentContext()];     UIImage * img = UIGraphicsGetImageFromCurrentImageContext();     UIGraphicsEndImageContext();     return img;}
查看完整描述

3 回答

?
慕容森

TA貢獻1853條經驗 獲得超18個贊


不使用UIGraphicsBeginImageContextUIGraphicsBeginImageContextWithOptions(如文件所示)在這頁上)。為Scale傳遞0.0(第三個參數),您將得到一個與屏幕的縮放因子相等的上下文。

UIGraphicsBeginImageContext使用的固定比例因子為1.0,因此在iPhone 4上的圖像實際上與其他iPhone上的圖像完全相同。我敢打賭,無論是iPhone 4在你隱式放大的時候應用了一個過濾器,還是你的大腦對它的感覺比它周圍的一切都不那么清晰。

所以,我猜:

#import <QuartzCore/QuartzCore.h>+ (UIImage *)imageWithView:(UIView *)view{
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];

    UIImage * img = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return img;}

在SWIFT 4中:

func image(with view: UIView) -> UIImage? {
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.isOpaque, 0.0)
    defer { UIGraphicsEndImageContext() }
    if let context = UIGraphicsGetCurrentContext() {
        view.layer.render(in: context)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        return image    }
    return nil}


查看完整回答
反對 回復 2019-06-14
?
至尊寶的傳說

TA貢獻1789條經驗 獲得超10個贊

我創建了一個基于@Dima解決方案的SWIFT擴展:

extension UIImage {
    class func imageWithView(view: UIView) -> UIImage {
        UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0)
        view.drawViewHierarchyInRect(view.bounds, afterScreenUpdates: true)
        let img = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return img    }}

編輯:SWIFT 4改進版

extension UIImage {
    class func imageWithView(_ view: UIView) -> UIImage {
        UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.isOpaque, 0)
        defer { UIGraphicsEndImageContext() }
        view.drawHierarchy(in: view.bounds, afterScreenUpdates: true)
        return UIGraphicsGetImageFromCurrentImageContext() ?? UIImage()
    }}

用法:

let view = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))  let image = UIImage.imageWithView(view)


查看完整回答
反對 回復 2019-06-14
  • 3 回答
  • 0 關注
  • 752 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號