使用clipToMask时,图像旋转90度 [英] Image is rotated 90 degrees when using clipToMask

查看:99
本文介绍了使用clipToMask时,图像旋转90度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to clip a portion of an image I take with a camera. Here's my clipping code:

- (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;

}

我正在ViewController中的captureNow方法中调用上述方法.我正在使用AVFoundation捕获静止图像:

I am calling above method in my captureNow method in my viewcontroller. I am using AVFoundation to capture still image:

-(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度.有谁知道为什么以及如何解决这个问题?预先感谢.

For some reason, my image is always rotated 90 degrees when I clip it. Does anyone know why and how to fix this? Thanks in advance.

推荐答案

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

Here alternative solution to rotate image to 90 degrees ,its code is bellow...

yourImageView.transform = CGAffineTransformMakeRotation(3.14159265/2);

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

when you want to share or save the picture at that time just rotate the Image With 90 Degrees and also if Required then 180 degree....

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

this is alternate solution if i got any other Idea then i will post again... hope,this help you...

:)

这篇关于使用clipToMask时,图像旋转90度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆