0

The app I am using for testing puposes is able to take a picture and save it as a PNG file. The next time the app is launched, it checks if a file is present and if it is, the image stored inside the file is used as the background view of the app. Up to this point all is OK.

I decided to add a clipping mask to this app and this where things go wrong. The clipping itself works, but for some mysterious reason the clipped image gets expanded. If someone could tell me what I am doing wrong that would be very helpful.

Here is the relevant code (I can provide more information if ever needed):

    if let videoConnection = stillImageOutput.connectionWithMediaType(AVMediaTypeVideo) {
        stillImageOutput.captureStillImageAsynchronouslyFromConnection(videoConnection) {
            (imageDataSampleBuffer, error) -> Void in
            if error == nil {
                var localImage = UIImage(fromSampleBuffer: imageDataSampleBuffer)
                var imageSize = CGSize(width: UIScreen.mainScreen().bounds.height * UIScreen.mainScreen().scale,
                    height: UIScreen.mainScreen().bounds.width * UIScreen.mainScreen().scale)
                localImage = resizeImage(localImage!, toSize: imageSize)

                imageSize = CGSize(width: imageSize.height, height: imageSize.width)
                UIGraphicsBeginImageContext(imageSize)
                CGContextRotateCTM (UIGraphicsGetCurrentContext(), CGFloat(M_PI_2))
                localImage!.drawAtPoint(CGPoint(x: 0.0, y: -imageSize.width))
                localImage = UIGraphicsGetImageFromCurrentImageContext()
                UIGraphicsEndImageContext()

                // Clipping code:
                localImage = maskImage(localImage!,
                    path: UIBezierPath(CGPath: CGPathCreateWithEllipseInRect(UIScreen.mainScreen().bounds, nil)))

                if let data = UIImagePNGRepresentation(localImage!) {
                    data.writeToFile(self.bmpFilePath, atomically: true)
                }
            } else {print("Error on taking a picture:\n\(error)")}
        }
    }

The maskImage function is this (taken from iOS UIImage clip to paths and translated to Swift) :

func maskImage(originalImage :UIImage, path:UIBezierPath) -> UIImage {
    UIGraphicsBeginImageContextWithOptions(originalImage.size, false, 0);
    path.addClip()
    originalImage.drawAtPoint(CGPointZero)
    let maskedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return maskedImage;
}

When the lines:

            localImage = maskImage(localImage!,
                path: UIBezierPath(CGPath: CGPathCreateWithEllipseInRect(UIScreen.mainScreen().bounds, nil)))

are commented out, I see what I expect.

Thaking the picture below and having it as background when relaunching the app.

enter image description here But when they are present(not commented out), I get the background hereafter when relaunching the app (of course taking the same picture at start):

enter image description here

If things worked as they should the mouse should appear inside the elliptic clip with the same size as in the first picture (not magnified as it is now).

Community
  • 1
  • 1
Michel
  • 10,303
  • 17
  • 82
  • 179
  • Sorry, I can not figure out What you are asking or what is your problem from anywhere in the question title or body (you should make that clear). – Nicolas Miari Jan 08 '16 at 03:37
  • Is it that the image is not centered in the clipped version? (2nd screenshot) – Nicolas Miari Jan 08 '16 at 03:38
  • I do not actually know if it is centered or not. But it is obviously magnified and this is the first problem. If I can avoid this magnifying phenomenon, it will the be easy to see if it is properly centered or not. At the end (when thing works -Sorry for not being clear on that-) the mouse should appear in the middle of the elliptic clip (same size as the first picture). – Michel Jan 08 '16 at 03:48
  • Magnified, yer; that's what I meant. Also seems position at top-left corner. – Nicolas Miari Jan 08 '16 at 03:49
  • Please edit your **question title / body** to make clearer ehat is the problem and/or what you want to achieve. – Nicolas Miari Jan 08 '16 at 03:49
  • Print `localImage.scale` before and after masking it. What do you get? – rob mayoff Jan 08 '16 at 03:49
  • Well yes, get rid of the magnification and then it will be easy to center or position. – Michel Jan 08 '16 at 03:50
  • If I print `localImage.scale` before and after masking it. I get 1.0 before and 2.0 after. – Michel Jan 08 '16 at 04:00
  • Hum? What does that really imply? The scale has changed, but the size of the image on the other hand has not changed a bit. – Michel Jan 08 '16 at 04:07
  • what r u doing in the resizeImage method ? – Teja Nandamuri Jan 08 '16 at 14:45
  • I can put the code for the resizeImage if necessary, but when I just comment out the code for maskImage, there is no magnifying issue any more. So I presume there is nothing wrong with resizeImage. – Michel Jan 09 '16 at 00:47

0 Answers0