0

我有一个 iOS 应用程序。我用相机拍了一张照片,然后保存了这张照片,然后用面具裁剪了这张照片。相机的第一张图像已正确保存,但是当我应用蒙版时,它以低分辨率和拉伸图像保存。

我在我的应用程序中使用这个 Objective-C 代码来应用掩码。

- (UIImage*) maskImage:(UIImage *)image withMask:(UIImage *)mask_Image {
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    //UIImage *maskImage = maskImage1;
    CGImageRef maskImageRef = [mask_Image CGImage];
    // create a bitmap graphics context the size of the image
    CGContextRef mainViewContentContext = CGBitmapContextCreate (NULL, mask_Image.size.width, mask_Image.size.height, 8, 0, colorSpace, kCGImageAlphaPremultipliedLast);
    if (mainViewContentContext==NULL)
        return NULL;
    CGFloat widthratio = 0;
    CGFloat heightratio = 0;
    widthratio = mask_Image.size.width / image.size.width;
    heightratio = mask_Image.size.height / image.size.height;
    CGRect rect1 = {{0, 0}, {mask_Image.size.width, mask_Image.size.height}};
    CGRect rect2 = {{-((image.size.width*widthratio)-mask_Image.size.width)/2 , -((image.size.height*heightratio)-mask_Image.size.height)/2}, {image.size.width*widthratio, image.size.height*heightratio}};
    CGContextClipToMask(mainViewContentContext, rect1, maskImageRef);
    CGContextDrawImage(mainViewContentContext, rect2, image.CGImage);
    // Create CGImageRef of the main view bitmap content, and then
    // release that bitmap context
    CGImageRef newImage = CGBitmapContextCreateImage(mainViewContentContext);
    CGContextRelease(mainViewContentContext);
    UIImage *theImage = [UIImage imageWithCGImage:newImage];
    CGImageRelease(newImage);
    // return the image

    NSData* imageData =  UIImagePNGRepresentation(theImage);     // get png representation
    UIImage* pngImage = [UIImage imageWithData:imageData];
    UIImageWriteToSavedPhotosAlbum(pngImage, nil, nil, nil);
    return theImage;
}

我想正确地得到这个:

  1. 我从相机拍下我的照片:

在此处输入图像描述

  1. 我将蒙版图像应用于所需位置的相机图像:

在此处输入图像描述

  1. 我得到了我的裁剪图像蒙版:

在此处输入图像描述

如何获得正确的蒙版图像?

4

0 回答 0