iphone - 使用clipToMask时图像旋转90度

标签 iphone ios quartz-2d cgimage

我正在尝试剪辑用相机拍摄的图像的一部分。这是我的剪辑代码:

- (UIImage*) maskImage:(UIImage *)image {

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

UIImage *maskImage = [UIImage imageNamed:@"mask3.png"];
CGImageRef maskImageRef = [maskImage CGImage];

// create a bitmap graphics context the size of the image
CGContextRef mainViewContentContext = CGBitmapContextCreate (NULL, maskImage.size.width, maskImage.size.height, 8, 0, colorSpace, kCGImageAlphaPremultipliedLast);


if (mainViewContentContext==NULL)
    return NULL;

CGFloat ratio = 0;

ratio = maskImage.size.width/ image.size.width;

if(ratio * image.size.height < maskImage.size.height) {
    ratio = maskImage.size.height/ image.size.height;
} 

CGRect rect1  = {{0, 0}, {maskImage.size.width, maskImage.size.height}};
CGRect rect2  = {{-((image.size.width*ratio)-maskImage.size.width)/2 , -((image.size.height*ratio)-maskImage.size.height)/2}, {image.size.width*ratio, image.size.height*ratio}};


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
return theImage;

我在 View Controller 的 captureNow 方法中调用上述方法。我正在使用 AVFoundation 捕获静态图像:

-(void) captureNow
{

AVCaptureConnection *videoConnection = nil;
for (AVCaptureConnection *connection in stillImageOutput.connections)
{
    for (AVCaptureInputPort *port in [connection inputPorts])
    {
        if ([[port mediaType] isEqual:AVMediaTypeVideo] )
        {
            videoConnection = connection;
            break;
        }
    }
    if (videoConnection) { break; }
}

NSLog(@"about to request a capture from: %@", stillImageOutput);



[stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error)
 {


     NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
     UIImage *image = [[UIImage alloc] initWithData:imageData];



     [testImageView setImage:[self maskImage:image]];


     self.firstPieceView.frame = CGRectMake(0, 0, 320, 480);

     self.firstPieceView.contentMode = UIViewContentModeScaleAspectFill;
   //  self.firstPieceView.image = image;

   //  [firstPieceView setNeedsDisplay];

     NSLog(@"self.firstPiece.frame is %@", NSStringFromCGRect(self.firstPieceView.frame));
    // [myView becomeFirstResponder];

     }];

self.previewParentView.hidden = YES;
self.photoBtn.hidden=YES;
self.imageMask.hidden = NO;

//add the gesture after taking the first image.
[myView addGestureRecognizersToPiece:self.myView];

由于某种原因,我的图像在剪辑时总是旋转 90 度。有谁知道为什么以及如何解决这个问题?提前致谢。

最佳答案

这是将图像旋转90度的替代解决方案,其代码如下...

yourImageView.transform = CGAffineTransformMakeRotation(3.14159265/2);

当您想共享或保存当时的图片时,只需将图像旋转 90 度,如果需要,也可以旋转 180 度....

这是替代解决方案,如果我有任何其他想法,那么我会再次发布...... 希望这对您有帮助...

:)

关于iphone - 使用clipToMask时图像旋转90度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6820014/

相关文章:

ios - 如何向上下文添加透明渐变蒙版

ios - 如何在 iphone 中点击应用程序图标时清除角标(Badge)计数器?

ios - 正确处理 32/64 位

iphone - iPod Touch CL 位置跟踪限制?

macos - CGPDFContext 和 CGPDFDocument 是什么关系?

swift - CTFontDrawGlyphs 在不正确的位置绘制

iphone - 在应用程序外部加载YouTube

ios - 如何在 dismissViewControllerAnimated 完成 block 之后立即执行方法?

iphone - 如何重新加载当前的 UIView

ios - 在 CGMutablePath 中添加多个排除路径