Selaa lähdekoodia

1.修改iOS 自定义相机与系统相机图像比例不一致。

Pen Li 7 vuotta sitten
vanhempi
commit
2a0b85366c

+ 48 - 13
common/Camera/Camera.storyboard

@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14460.31" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
-    <device id="retina4_7" orientation="portrait">
+    <device id="retina5_5" orientation="portrait">
         <adaptation id="fullscreen"/>
     </device>
     <dependencies>
@@ -15,18 +15,18 @@
             <objects>
                 <viewController storyboardIdentifier="RACameraViewController" useStoryboardIdentifierAsRestorationIdentifier="YES" id="u3G-8E-Sj8" customClass="RACameraViewController" sceneMemberID="viewController">
                     <view key="view" contentMode="scaleToFill" id="ZFi-1H-xB4">
-                        <rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
+                        <rect key="frame" x="0.0" y="0.0" width="414" height="736"/>
                         <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
                         <subviews>
                             <view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="aIR-II-sqh">
-                                <rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
+                                <rect key="frame" x="0.0" y="0.0" width="414" height="736"/>
                                 <color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
                             </view>
                             <view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="2cM-nE-ueF">
-                                <rect key="frame" x="0.0" y="20" width="375" height="647"/>
+                                <rect key="frame" x="0.0" y="20" width="414" height="716"/>
                                 <subviews>
                                     <button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="Zmb-bI-WjK">
-                                        <rect key="frame" x="157.5" y="527" width="60" height="60"/>
+                                        <rect key="frame" x="177" y="626" width="60" height="60"/>
                                         <constraints>
                                             <constraint firstAttribute="width" constant="60" id="Jgr-Dk-QIx"/>
                                             <constraint firstAttribute="width" secondItem="Zmb-bI-WjK" secondAttribute="height" multiplier="1:1" id="MQN-fF-lQY"/>
@@ -45,12 +45,46 @@
                                 </subviews>
                                 <color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
                                 <constraints>
-                                    <constraint firstAttribute="bottom" secondItem="Zmb-bI-WjK" secondAttribute="bottom" constant="60" id="f8Y-IT-5Vo"/>
+                                    <constraint firstItem="Zmb-bI-WjK" firstAttribute="centerY" secondItem="2cM-nE-ueF" secondAttribute="centerY" id="5lq-gs-zZH"/>
+                                    <constraint firstAttribute="bottom" secondItem="Zmb-bI-WjK" secondAttribute="bottom" constant="30" id="f8Y-IT-5Vo"/>
                                     <constraint firstItem="Zmb-bI-WjK" firstAttribute="centerX" secondItem="2cM-nE-ueF" secondAttribute="centerX" id="wR7-zz-eVK"/>
+                                    <constraint firstAttribute="trailing" secondItem="Zmb-bI-WjK" secondAttribute="trailing" constant="30" id="yRs-XW-gWU"/>
                                 </constraints>
+                                <variation key="default">
+                                    <mask key="constraints">
+                                        <exclude reference="5lq-gs-zZH"/>
+                                        <exclude reference="f8Y-IT-5Vo"/>
+                                        <exclude reference="wR7-zz-eVK"/>
+                                        <exclude reference="yRs-XW-gWU"/>
+                                    </mask>
+                                </variation>
+                                <variation key="heightClass=compact-widthClass=compact">
+                                    <mask key="constraints">
+                                        <include reference="5lq-gs-zZH"/>
+                                        <include reference="yRs-XW-gWU"/>
+                                    </mask>
+                                </variation>
+                                <variation key="heightClass=compact-widthClass=regular">
+                                    <mask key="constraints">
+                                        <include reference="5lq-gs-zZH"/>
+                                        <include reference="yRs-XW-gWU"/>
+                                    </mask>
+                                </variation>
+                                <variation key="heightClass=regular-widthClass=compact">
+                                    <mask key="constraints">
+                                        <include reference="f8Y-IT-5Vo"/>
+                                        <include reference="wR7-zz-eVK"/>
+                                    </mask>
+                                </variation>
+                                <variation key="heightClass=regular-widthClass=regular">
+                                    <mask key="constraints">
+                                        <include reference="f8Y-IT-5Vo"/>
+                                        <include reference="wR7-zz-eVK"/>
+                                    </mask>
+                                </variation>
                             </view>
                         </subviews>
-                        <color key="backgroundColor" red="0.76354031735751293" green="0.76354031735751293" blue="0.76354031735751293" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
+                        <color key="backgroundColor" white="0.0" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
                         <constraints>
                             <constraint firstItem="aIR-II-sqh" firstAttribute="leading" secondItem="ZFi-1H-xB4" secondAttribute="leading" id="1yp-i3-hXU"/>
                             <constraint firstItem="aIR-II-sqh" firstAttribute="width" secondItem="ZFi-1H-xB4" secondAttribute="width" id="3jE-Zo-qAc"/>
@@ -66,6 +100,7 @@
                         <viewLayoutGuide key="safeArea" id="fLT-zm-dh6"/>
                     </view>
                     <connections>
+                        <outlet property="backBtn" destination="mbh-f5-Pjk" id="h8n-RN-SL7"/>
                         <outlet property="controlContanier" destination="2cM-nE-ueF" id="8g4-KY-DMK"/>
                         <outlet property="previewContainer" destination="aIR-II-sqh" id="Xhl-yB-wfI"/>
                         <outlet property="takePhotoBtn" destination="Zmb-bI-WjK" id="4ND-ng-dvs"/>
@@ -84,14 +119,14 @@
                         <viewControllerLayoutGuide type="bottom" id="1aO-Vd-AQh"/>
                     </layoutGuides>
                     <view key="view" contentMode="scaleToFill" id="khP-jm-wId">
-                        <rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
+                        <rect key="frame" x="0.0" y="0.0" width="414" height="736"/>
                         <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
                         <subviews>
                             <scrollView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleAspectFit" showsHorizontalScrollIndicator="NO" showsVerticalScrollIndicator="NO" maximumZoomScale="4" translatesAutoresizingMaskIntoConstraints="NO" id="w4v-z1-b7B">
-                                <rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
+                                <rect key="frame" x="0.0" y="0.0" width="414" height="736"/>
                                 <subviews>
                                     <imageView userInteractionEnabled="NO" contentMode="scaleAspectFit" horizontalHuggingPriority="251" verticalHuggingPriority="251" translatesAutoresizingMaskIntoConstraints="NO" id="Z0q-fB-hKK">
-                                        <rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
+                                        <rect key="frame" x="0.0" y="0.0" width="414" height="736"/>
                                     </imageView>
                                 </subviews>
                                 <constraints>
@@ -107,14 +142,14 @@
                                 </connections>
                             </scrollView>
                             <view hidden="YES" alpha="0.44999998807907099" contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="WfP-Fk-mo9">
-                                <rect key="frame" x="0.0" y="605" width="375" height="62"/>
+                                <rect key="frame" x="0.0" y="674" width="414" height="62"/>
                                 <color key="backgroundColor" white="0.33333333329999998" alpha="1" colorSpace="calibratedWhite"/>
                                 <constraints>
                                     <constraint firstAttribute="height" constant="62" id="cnn-ba-VJB"/>
                                 </constraints>
                             </view>
                             <button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="XlI-8n-qRu">
-                                <rect key="frame" x="50" y="532" width="60" height="60"/>
+                                <rect key="frame" x="50" y="601" width="60" height="60"/>
                                 <color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
                                 <fontDescription key="fontDescription" type="system" pointSize="15"/>
                                 <state key="normal" image="btn_photo_retake"/>
@@ -123,7 +158,7 @@
                                 </connections>
                             </button>
                             <button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="ZBU-jX-GTo">
-                                <rect key="frame" x="265" y="532" width="60" height="60"/>
+                                <rect key="frame" x="304" y="601" width="60" height="60"/>
                                 <fontDescription key="fontDescription" type="system" pointSize="15"/>
                                 <state key="normal" image="btn_photo_assign"/>
                                 <connections>

+ 4 - 0
common/Camera/RACameraViewController.h

@@ -7,8 +7,10 @@
 //
 
 #import <UIKit/UIKit.h>
+#import <AVFoundation/AVAnimation.h>
 #import "RACamera.h"
 
+
 @interface RACameraViewController : UIViewController
 
 @property (nonatomic,copy) void (^completion)(UIImage *image);
@@ -19,4 +21,6 @@
 
 + (instancetype)viewControllerFromStoryboard;
 
++ (instancetype)showCameraFromViewController:(UIViewController *)from withTakeMode:(RACameraTakeMode)mode videoGravity:(AVLayerVideoGravity)gravity completion:(void(^)(UIImage *image))completion;
+
 @end

+ 50 - 2
common/Camera/RACameraViewController.m

@@ -15,6 +15,7 @@
 @property (nonatomic,strong) IBOutlet UIView *previewContainer;
 @property (nonatomic,strong) IBOutlet UIView *controlContanier;
 @property (strong, nonatomic) IBOutlet UIButton *takePhotoBtn;
+@property (strong, nonatomic) IBOutlet UIButton *backBtn;
 
 @property (nonatomic,assign) BOOL barHidden;
 
@@ -27,6 +28,7 @@
 @property (nonatomic,strong) AVCaptureVideoPreviewLayer *previewLayer;
 
 @property (nonatomic,assign) BOOL cameraInitial;
+@property (nonatomic,assign) AVLayerVideoGravity gravity;
 
 @end
 
@@ -39,9 +41,22 @@
 + (instancetype)viewControllerFromStoryboard {
     RACameraViewController *cameraVC = [[UIStoryboard storyboardWithName:@"Camera" bundle:nil] instantiateViewControllerWithIdentifier:[self storyboardID]];
     cameraVC.takeMode = RACameraTakeModeTakeOnce;
+    cameraVC.gravity = AVLayerVideoGravityResizeAspectFill;
     return cameraVC;
 }
 
++ (instancetype)showCameraFromViewController:(UIViewController *)from withTakeMode:(RACameraTakeMode)mode videoGravity:(AVLayerVideoGravity)gravity completion:(void(^)(UIImage *image))completion {
+    
+    RACameraViewController *vc = [self viewControllerFromStoryboard];
+    vc.takeMode = mode;
+    if (!gravity) {
+        gravity = AVLayerVideoGravityResizeAspectFill;
+    }
+    vc.gravity = gravity;
+    vc.completion = completion;
+    return vc;
+}
+
 - (void)viewDidLoad {
     [super viewDidLoad];
     // Do any additional setup after loading the view.
@@ -142,9 +157,42 @@
 - (void)viewDidLayoutSubviews {
     [super viewDidLayoutSubviews];
     
+    // 强制布局一次,不然 takePhotoBtn frame还没发生改变
+    [self.controlContanier layoutIfNeeded];
+    
     if ([self camerAuthorization] && self.cameraInitial) {
         
-        [self resetPreview:self.previewContainer.bounds];
+        CGRect frame = self.previewContainer.bounds;
+        
+        BOOL isPortrait = self.interfaceOrientation == UIInterfaceOrientationPortrait || self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown;
+        // aspect ratio 3:4
+        if (self.gravity == AVLayerVideoGravityResizeAspect) {
+            
+            CGRect backBtnFrame = [self.previewContainer convertRect:self.backBtn.frame fromView:self.controlContanier];
+            CGRect captureBtnFrame = [self.previewContainer convertRect:self.takePhotoBtn.frame fromView:self.controlContanier];
+            
+            if (isPortrait) {
+                CGFloat left = 0;
+                CGFloat top = CGRectGetMaxY(backBtnFrame);
+                CGFloat bottom = CGRectGetMinY(captureBtnFrame);
+                CGFloat width = CGRectGetWidth(self.controlContanier.bounds);
+                CGFloat height = bottom - top;
+                
+                frame = CGRectMake(left, top, width, height);
+            } else {
+                
+                CGFloat left = CGRectGetMaxX(backBtnFrame);
+                CGFloat right = CGRectGetMinX(captureBtnFrame);
+                CGFloat top = 0;
+                CGFloat width = right - left;
+                CGFloat bottom = CGRectGetHeight(self.controlContanier.bounds);
+                CGFloat height = bottom - top;
+                
+                frame = CGRectMake(left, top, width, height);
+            }
+        }
+        
+        [self resetPreview:frame];
     }
 }
 
@@ -253,7 +301,7 @@
     [self.captureSession commitConfiguration];
     
     self.previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.captureSession];
-    self.previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
+    self.previewLayer.videoGravity = self.gravity;
     [self.previewContainer.layer addSublayer:self.previewLayer];
     
     self.cameraInitial = YES;

+ 1 - 1
common/Camera/RATakePhotoPreviewController.m

@@ -7,7 +7,7 @@
 //
 
 #import "RATakePhotoPreviewController.h"
-#import "const.h"
+//#import "const.h"
 
 @interface RATakePhotoPreviewController ()<UIScrollViewDelegate>