To do this you can use the Vision framework along with Core Image. It involves the following steps:
Set up the camera capture session-Use the AVCaptureSession for this.
Perform edge detection using VNDetectRectanglesRequest.
Process the detected rectangle.
Crop and perspective-correct the image.