English 中文(简体)
我如何反映来自UIImagePickerController的UIImage图像
原标题:How do I mirror a UIImage picture from UIImagePickerController

如果有任何办法反映某一形象,我就试图说出。 例如,了解一个人的面貌,然后将其割断一半,显示他们与对方的面貌。 似乎没有像CGAffineTransform职能那样的trick。 制图专家请帮助!

问题回答

这里的基本“骗局”是指对X或Y轴进行1级系数的微调。 例如,你可以利用这一方法创造“横向轴心”转变:

CGAffineTransform transform = CGAffineTransformScale(transform, -1, 1);

然后,你可以将<代码>transform上的财产贴在上,以示所分配的形象,或用另一种转变加以压缩,以发挥更为复杂的作用。 为了取得你所描述的准确效果,你可能需要写一些习俗图,以把你原来的形象带入一个背景,然后把 f倒在后面的半部。 这在核心图表中相对简单。

如果你只计划支持4.0+

UIImageOrientation flippedOrientation = UIImageOrientationUpMirrored;
switch (image.imageOrientation) {
  case UIImageOrientationUp: break;
  case UIImageOrientationDown: flippedOrientation = UIImageOrientationDownMirrored; break;
  // ...
}
UIImage * flippedImage = [UIImage imageWithCGImage:image.CGImage scale:image.scale orientation:flippedOrientation];

你们也许会想到,为什么双方都发表了令人愤慨的长期开关声明?

? UIImage *flip = [UIImage imageWithCGImage:image.CGImage   
?                                     scale:image.scale
?                               orientation:(image.imageOrientation + 4) % 8];

如果你看一看一下,你就可以看到作一番算法会做到:

typedef NS_ENUM(NSInteger, UIImageOrientation) {
    UIImageOrientationUp,            // default orientation
    UIImageOrientationDown,          // 180 deg rotation
    UIImageOrientationLeft,          // 90 deg CCW
    UIImageOrientationRight,         // 90 deg CW
    UIImageOrientationUpMirrored,    // as above but image mirrored along other axis. horizontal flip
    UIImageOrientationDownMirrored,  // horizontal flip
    UIImageOrientationLeftMirrored,  // vertical flip
    UIImageOrientationRightMirrored, // vertical flip
};

但这一法典太紧。 你们应该用明确的开关声明来写一份职务。 E.g.

UIImageOrientation mirroredImageOrientation(UIImageOrientation orientation) {
    switch(orientation) {
        case UIImageOrientationUp: return UIImageOrientationUpMirrored;
        case UIImageOrientationDown: return UIImageOrientationDownMirrored;
        case UIImageOrientationLeft: return UIImageOrientationLeftMirrored;
        case UIImageOrientationRight: return UIImageOrientationRightMirrored;
        case UIImageOrientationUpMirrored: return UIImageOrientationUp;
        case UIImageOrientationDownMirrored: return UIImageOrientationDown;
        case UIImageOrientationLeftMirrored: return UIImageOrientationLeft;
        case UIImageOrientationRightMirrored: return UIImageOrientationRight;
        default: return orientation;
    }
}

并且利用这种职能:

UIImage *flip = [UIImage imageWithCGImage:image.CGImage   
                                    scale:image.scale
                              orientation:mirroredImageOrientation(image.imageOrientation)];

我添加了问题标记,以表明有疑问的精炼法。 类似

上述答复中,没有一个回答问题部分,而问题部分反映的是一半的图像,并不影响整个形象。 解决办法的混合导致以下样本功能:你可用作UIImage+等类。 镜子:

(UIImage *) horizontalMirror {
    UIImageOrientation flippedOrientation = UIImageOrientationUpMirrored;
    switch (self.imageOrientation) {
        case UIImageOrientationUp: break;
        case UIImageOrientationDown: flippedOrientation = UIImageOrientationDownMirrored; break;
    }
    UIImage * flippedImage = [UIImage imageWithCGImage:self.CGImage scale:1.0 orientation:flippedOrientation];

    CGImageRef inImage = self.CGImage;
    CGContextRef ctx = CGBitmapContextCreate(NULL,
                                             CGImageGetWidth(inImage),
                                             CGImageGetHeight(inImage),
                                             CGImageGetBitsPerComponent(inImage),
                                             CGImageGetBytesPerRow(inImage),
                                             CGImageGetColorSpace(inImage),
                                             CGImageGetBitmapInfo(inImage)
                                             );
    CGRect cropRect = CGRectMake(flippedImage.size.width/2, 0, flippedImage.size.width/2, flippedImage.size.height);
    CGImageRef TheOtherHalf = CGImageCreateWithImageInRect(flippedImage.CGImage, cropRect);
    CGContextDrawImage(ctx, CGRectMake(0, 0, CGImageGetWidth(inImage), CGImageGetHeight(inImage)), inImage);

    CGAffineTransform transform = CGAffineTransformMakeTranslation(flippedImage.size.width, 0.0);
    transform = CGAffineTransformScale(transform, -1.0, 1.0);
    CGContextConcatCTM(ctx, transform);

    CGContextDrawImage(ctx, cropRect, TheOtherHalf);

    CGImageRef imageRef = CGBitmapContextCreateImage(ctx);
    CGContextRelease(ctx);
    UIImage *finalImage = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);

    return finalImage;
}

更便于使用:

UIImage(assetIdentifier: .myIcon)?.withHorizontallyFlippedOrientation()

页: 1

 func didTakePicture(_ real_image: UIImage) {
   //suppose real_image = :)
 var flipped_image = UIImage(CGImage: real_image.CGImage!, scale: real_image.scale, orientation: .leftMirrored)
  // flipped_image is  (:
  }




相关问题
Code sign Error

I have created a new iPhone application.I have two mach machines. I have created the certificate for running application in iPhone in one mac. Can I use the other mac for running the application in ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

将音频Clips从Peter改为服务器

我不禁要问,那里是否有任何实例表明从Peit向服务器发送音响。 I m不关心电话或SIP风格的解决办法,只是一个简单的袖珍流程......

• 如何将搜查线重新定位?

我正试图把图像放在搜索条左边。 但是,问题始于这里,搜索条线不能重新布署。

热门标签