Cropping image captured by AVCaptureSession

后端 未结 3 1266
灰色年华
灰色年华 2020-12-17 17:52

I\'m writing an iPhone App which uses AVFoundation to take a photo and crop it. The App is similar to a QR code reader: It uses a AVCaptureVideoPreviewLayer with an overlay.

3条回答
  •  春和景丽
    2020-12-17 18:29

    Hope this meets your requirements

    - (UIImage *)cropImage:(UIImage *)image to:(CGRect)cropRect andScaleTo:(CGSize)size {
        UIGraphicsBeginImageContext(size);
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGImageRef subImage = CGImageCreateWithImageInRect([image CGImage], cropRect);
        NSLog(@"---------");     
        NSLog(@"*cropRect.origin.y=%f",cropRect.origin.y);
        NSLog(@"*cropRect.origin.x=%f",cropRect.origin.x);
    
        NSLog(@"*cropRect.size.width=%f",cropRect.size.width);     
        NSLog(@"*cropRect.size.height=%f",cropRect.size.height);     
    
        NSLog(@"---------");     
    
        NSLog(@"*size.width=%f",size.width);     
        NSLog(@"*size.height=%f",size.height);     
    
        CGRect myRect = CGRectMake(0.0f, 0.0f, size.width, size.height);
        CGContextScaleCTM(context, 1.0f, -1.0f);
        CGContextTranslateCTM(context, 0.0f, -size.height);
        CGContextDrawImage(context, myRect, subImage);
        UIImage* croppedImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        CGImageRelease(subImage);     
    
        return croppedImage;
    }
    

提交回复
热议问题