swift解決present相機(jī)導(dǎo)航欄顏色問題

問題1摩窃、最近一直在開發(fā)swift項目,遇到一個問題芬骄,就是我們項目的導(dǎo)航欄文字是白色猾愿,當(dāng)我們present進(jìn)入相冊中時, 會發(fā)現(xiàn)相冊導(dǎo)航欄的文字顏色也是白色账阻, 無法看到蒂秘。解決這個問題,只需要我們在加載UIImagePickerController淘太,對相冊導(dǎo)航欄進(jìn)行設(shè)置即可:

fileprivate lazy var imagePicker : UIImagePickerController = {
        let imgPicker = UIImagePickerController()
        // 允許編輯
        imgPicker.allowsEditing = true
        // 導(dǎo)航欄字體顏色
        imgPicker.navigationBar.tintColor = UIColor.black
        let dict:NSDictionary = [NSAttributedStringKey.foregroundColor: UIColor.black,NSAttributedStringKey.font : UIFont.boldSystemFont(ofSize: 18)]
        //標(biāo)題設(shè)置顏色與字體大小
        imgPicker.navigationBar.titleTextAttributes = dict as? [NSAttributedStringKey : AnyObject]
        //設(shè)置代理姻僧,檢測操作
        imgPicker.delegate = self as UIImagePickerControllerDelegate & UINavigationControllerDelegate
        return imgPicker
    }()

問題2规丽、解決拍照自動旋轉(zhuǎn)90度問題
OC版本代碼:

- (UIImage *)fixOrientation:(UIImage *)aImage {
    // No-op if the orientation is already correct
    if (aImage.imageOrientation == UIImageOrientationUp)
        return aImage;
    
    // We need to calculate the proper transformation to make the image upright.
    // We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored.
    CGAffineTransform transform = CGAffineTransformIdentity;
    
    switch (aImage.imageOrientation) {
        case UIImageOrientationDown:
        case UIImageOrientationDownMirrored:
            transform = CGAffineTransformTranslate(transform, aImage.size.width, aImage.size.height);
            transform = CGAffineTransformRotate(transform, M_PI);
            break;
            
        case UIImageOrientationLeft:
        case UIImageOrientationLeftMirrored:
            transform = CGAffineTransformTranslate(transform, aImage.size.width, 0);
            transform = CGAffineTransformRotate(transform, M_PI_2);
            break;
            
        case UIImageOrientationRight:
        case UIImageOrientationRightMirrored:
            transform = CGAffineTransformTranslate(transform, 0, aImage.size.height);
            transform = CGAffineTransformRotate(transform, -M_PI_2);
            break;
        default:
            break;
    }
    
    switch (aImage.imageOrientation) {
        case UIImageOrientationUpMirrored:
        case UIImageOrientationDownMirrored:
            transform = CGAffineTransformTranslate(transform, aImage.size.width, 0);
            transform = CGAffineTransformScale(transform, -1, 1);
            break;
            
        case UIImageOrientationLeftMirrored:
        case UIImageOrientationRightMirrored:
            transform = CGAffineTransformTranslate(transform, aImage.size.height, 0);
            transform = CGAffineTransformScale(transform, -1, 1);
            break;
        default:
            break;
    }
    
    // Now we draw the underlying CGImage into a new context, applying the transform
    // calculated above.
    CGContextRef ctx = CGBitmapContextCreate(NULL, aImage.size.width, aImage.size.height,
                                             CGImageGetBitsPerComponent(aImage.CGImage), 0,
                                             CGImageGetColorSpace(aImage.CGImage),
                                             CGImageGetBitmapInfo(aImage.CGImage));
    CGContextConcatCTM(ctx, transform);
    switch (aImage.imageOrientation) {
        case UIImageOrientationLeft:
        case UIImageOrientationLeftMirrored:
        case UIImageOrientationRight:
        case UIImageOrientationRightMirrored:
            // Grr...
            CGContextDrawImage(ctx, CGRectMake(0,0,aImage.size.height,aImage.size.width), aImage.CGImage);
            break;
            
        default:
            CGContextDrawImage(ctx, CGRectMake(0,0,aImage.size.width,aImage.size.height), aImage.CGImage);
            break;
    }
    
    // And now we just create a new UIImage from the drawing context
    CGImageRef cgimg = CGBitmapContextCreateImage(ctx);
    UIImage *img = [UIImage imageWithCGImage:cgimg];
    CGContextRelease(ctx);
    CGImageRelease(cgimg);
    return img;
}

Swift版本代碼:

/// 解決拍照自動旋轉(zhuǎn)90度問題
    ///
    /// - Parameter aImage: <#aImage description#>
    public final func fixOrientation(aImage: UIImage) -> UIImage {
        // 取向正確,則不操作
        if aImage.imageOrientation == UIImageOrientation.up {
            return aImage
        }
        
        //通過合理的計算使用正確的取向圖片
        //我們用2步驟:如果左/右/向下旋轉(zhuǎn),然后讓鏡像翻轉(zhuǎn)
        var transform = CGAffineTransform.identity
        
        switch aImage.imageOrientation {
        case .down, .downMirrored:
            transform = transform.translatedBy(x: aImage.size.width, y: aImage.size.height)
            transform = transform.rotated(by: .pi)
        case .left, .leftMirrored:
            transform = transform.translatedBy(x: aImage.size.width, y: 0)
            transform = transform.rotated(by: .pi / 2)
        case .right, .rightMirrored:
            transform = transform.translatedBy(x: 0, y: aImage.size.height)
            transform = transform.rotated(by: -.pi / 2)
        default:break
        }
        
        switch aImage.imageOrientation {
        case .upMirrored, .downMirrored:
            transform = transform.translatedBy(x: aImage.size.width, y: 0)
            transform = transform.scaledBy(x: -1, y: 1)
        case .leftMirrored, .rightMirrored:
            transform = transform.translatedBy(x: aImage.size.height, y: 0)
            transform = transform.scaledBy(x: -1, y: 1)
        default:break
        }
        
        let ctx = CGContext(data: nil, width: Int(aImage.size.width), height: Int(aImage.size.height), bitsPerComponent: aImage.cgImage!.bitsPerComponent, bytesPerRow: 0, space: aImage.cgImage!.colorSpace!, bitmapInfo: aImage.cgImage!.bitmapInfo.rawValue)
        ctx?.concatenate(transform)
        
        switch aImage.imageOrientation {
        case .left, .leftMirrored, .right, .rightMirrored:
            ctx?.draw(aImage.cgImage!, in: CGRect(x: CGFloat(0), y: CGFloat(0), width: CGFloat(size.height), height: CGFloat(size.width)))
        default:
            ctx?.draw(aImage.cgImage!, in: CGRect(x: CGFloat(0), y: CGFloat(0), width: CGFloat(size.width), height: CGFloat(size.height)))
        }
        
        let cgimg: CGImage = (ctx?.makeImage())!
        let img = UIImage(cgImage: cgimg)
        
        return img
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末撇贺,一起剝皮案震驚了整個濱河市赌莺,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌显熏,老刑警劉巖雄嚣,帶你破解...
    沈念sama閱讀 222,807評論 6 518
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異喘蟆,居然都是意外死亡缓升,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,284評論 3 399
  • 文/潘曉璐 我一進(jìn)店門蕴轨,熙熙樓的掌柜王于貴愁眉苦臉地迎上來港谊,“玉大人,你說我怎么就攤上這事橙弱∑缢拢” “怎么了?”我有些...
    開封第一講書人閱讀 169,589評論 0 363
  • 文/不壞的土叔 我叫張陵棘脐,是天一觀的道長斜筐。 經(jīng)常有香客問我,道長蛀缝,這世上最難降的妖魔是什么顷链? 我笑而不...
    開封第一講書人閱讀 60,188評論 1 300
  • 正文 為了忘掉前任,我火速辦了婚禮屈梁,結(jié)果婚禮上嗤练,老公的妹妹穿的比我還像新娘。我一直安慰自己在讶,他們只是感情好煞抬,可當(dāng)我...
    茶點故事閱讀 69,185評論 6 398
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著构哺,像睡著了一般革答。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上曙强,一...
    開封第一講書人閱讀 52,785評論 1 314
  • 那天残拐,我揣著相機(jī)與錄音,去河邊找鬼旗扑。 笑死蹦骑,一個胖子當(dāng)著我的面吹牛慈省,可吹牛的內(nèi)容都是我干的臀防。 我是一名探鬼主播眠菇,決...
    沈念sama閱讀 41,220評論 3 423
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼袱衷!你這毒婦竟也來了捎废?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 40,167評論 0 277
  • 序言:老撾萬榮一對情侶失蹤致燥,失蹤者是張志新(化名)和其女友劉穎登疗,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體嫌蚤,經(jīng)...
    沈念sama閱讀 46,698評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡辐益,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,767評論 3 343
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了脱吱。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片智政。...
    茶點故事閱讀 40,912評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖箱蝠,靈堂內(nèi)的尸體忽然破棺而出续捂,到底是詐尸還是另有隱情,我是刑警寧澤宦搬,帶...
    沈念sama閱讀 36,572評論 5 351
  • 正文 年R本政府宣布牙瓢,位于F島的核電站,受9級特大地震影響间校,放射性物質(zhì)發(fā)生泄漏矾克。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 42,254評論 3 336
  • 文/蒙蒙 一撇簿、第九天 我趴在偏房一處隱蔽的房頂上張望聂渊。 院中可真熱鬧,春花似錦四瘫、人聲如沸汉嗽。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,746評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽饼暑。三九已至,卻和暖如春洗做,著一層夾襖步出監(jiān)牢的瞬間弓叛,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,859評論 1 274
  • 我被黑心中介騙來泰國打工诚纸, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留撰筷,地道東北人。 一個月前我還...
    沈念sama閱讀 49,359評論 3 379
  • 正文 我出身青樓畦徘,卻偏偏與公主長得像毕籽,于是被迫代替她去往敵國和親抬闯。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,922評論 2 361

推薦閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫关筒、插件溶握、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,124評論 4 61
  • 文、攝影:垂楊紫陌 我一直在做一個夢 夢很長 我穿梭在前世今生來世 找自己 一個無面人 好多無面人 從時間的縫隙里...
    love垂楊紫陌閱讀 149評論 2 2
  • 注:這篇文章已被作者標(biāo)注為“糟糕的文章”蒸播,不建議參考和閱讀 情景:現(xiàn)在有2個分支睡榆,master和dev。dev是從...
    KardelShaw閱讀 270評論 0 0
  • B超檢查 B超上的圖片袍榆,對于我們來說胀屿,比較難接收,所以我們只需要看下邊的字包雀。 ▌子宮肌瘤:B超單上會寫有宮體增大碉纳,...
    肖紅千金閱讀 11,649評論 0 0