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];
-
拍照
- (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; }); } } -
拍照处理
- (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; }); }); }