PHAuthorizationStatusLimited 选择更多照片

1.如果不想每次都弹出系统的弹窗,需要在info.plist中添加Prevent limited photos access alert 为YES

2.自己控制选择添加更多 :

[[PHPhotoLibrary sharedPhotoLibrary] presentLimitedLibraryPickerFromViewController:self
        completionHandler:^(NSArray<NSString *> *newlySelectedAssetIdentifiers) {
     [weakSelf loadAllAssets];  // 刷新数据
}];

3.在Link Binary With Libraries 中添加PhotosUI.framework ,否则程序会闪退

4.旋转照片

- (UIImage *)rotateImageByChangingOrientation:(UIImage *)image {
  if (!image) return nil;

    UIImageOrientation currentOrientation = image.imageOrientation;
    UIImageOrientation newOrientation;

  switch (currentOrientation) {
    case UIImageOrientationUp:
        newOrientation = UIImageOrientationRight;
        break;
    case UIImageOrientationRight:
        newOrientation = UIImageOrientationDown;
        break;
    case UIImageOrientationDown:
        newOrientation = UIImageOrientationLeft;
        break;
    case UIImageOrientationLeft:
        newOrientation = UIImageOrientationUp;
        break;
    default:
        newOrientation = UIImageOrientationUp;
        break;
 }

  return [UIImage imageWithCGImage:image.CGImage
                          scale:image.scale
                    orientation:newOrientation];
}

5.调用旋转照片

self.capturedImage = [self rotateImageByChangingOrientation:self.capturedImage];

6.压缩照片

- (UIImage *)compressImage:(UIImage *)image toMaxSize:(NSInteger)maxSize {
CGFloat compression = 1.0;
NSData *imageData = UIImageJPEGRepresentation(image, compression);
NSLog(@"原图%.2fMB",(float)(imageData.length/1024.0/1024));
if (!imageData) return image;

if (imageData.length <= maxSize) {
    return image;
}

CGFloat min = 0.0;
CGFloat max = 1.0;
for (int i = 0; i < 8; i++) {
    compression = (min + max) / 2;
    imageData = UIImageJPEGRepresentation(image, compression);
    if (imageData.length < maxSize) {
        min = compression;
    } else {
        max = compression;
    }
}

imageData = UIImageJPEGRepresentation(image, min);
NSLog(@"压缩后图片%.2fMB",(float)(imageData.length/1024.0/1024));
return [[UIImage alloc] initWithData:imageData];
}

7.调用

UIImage *image = [self compressImage:self.resultImageView.image toMaxSize:1.8 * 1024 * 1024];
  1. 拍照

    - (void)setupCamera {
       self.session = [[AVCaptureSession alloc] init];
       self.session.sessionPreset = AVCaptureSessionPresetPhoto;
    
       AVCaptureDevice *backCamera = [AVCaptureDevice defaultDeviceWithDeviceType:AVCaptureDeviceTypeBuiltInWideAngleCamera mediaType:AVMediaTypeVideo position:AVCaptureDevicePositionBack];
    
    // 开启连续自动对焦,防抖防糊
     if ([backCamera isFocusModeSupported:AVCaptureFocusModeContinuousAutoFocus]) {
         [backCamera lockForConfiguration:nil];
         backCamera.focusMode = AVCaptureFocusModeContinuousAutoFocus;
         [backCamera unlockForConfiguration];
     }
    
      NSError *error;
     AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:backCamera error:&error];
     if ([self.session canAddInput:input]) {
        [self.session addInput:input];
     }
    
     self.photoOutput = [[AVCapturePhotoOutput alloc] init];
     if ([self.session canAddOutput:self.photoOutput]) {
          [self.session addOutput:self.photoOutput];
       }
    
      self.previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:self.session];
      self.previewLayer.frame = self.view.bounds;
      self.previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
      [self.view.layer addSublayer:self.previewLayer];
    }
    
     - (void)startSession {
       if (_session && !_session.isRunning) {
           dispatch_async(dispatch_get_global_queue(0, 0), ^{
              [self.session startRunning];
              self.isSessionRunning = YES;
        });
        }
     }
    
    - (void)stopSession {
       if (_session && _session.isRunning) {
         dispatch_async(dispatch_get_global_queue(0, 0), ^{
           [self.session stopRunning];
           self.isSessionRunning = NO;
        });
      }
    }
    
  2. 拍照处理

    - (void)captureAction {
        if (!self.isSessionRunning) return;
        AVCapturePhotoSettings *settings = [AVCapturePhotoSettings photoSettings];
        [self.photoOutput capturePhotoWithSettings:settings delegate:self];
    }
    
      - (void)captureOutput:(AVCapturePhotoOutput *)output didFinishProcessingPhoto:(AVCapturePhoto *)photo error:(NSError *)error {
          if (error || !photo) return;
    
           __weak typeof(self) weakSelf = self;
          dispatch_async(dispatch_get_global_queue(0, 0), ^{
                NSData *data = photo.fileDataRepresentation;
                UIImage *image = [UIImage imageWithData:data];
        
                 dispatch_async(dispatch_get_main_queue(), ^{
                   self.rotateAngle = 0;
                   weakSelf.capturedImage = image;
                   weakSelf.resultImageView.image = image;
                   weakSelf.resultView.hidden = NO;
                   weakSelf.guideContainer.hidden = YES;
               });
           });
     }
    
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容