2011-07-25 64 views
0

我想剪輯一部分使用相機拍攝的圖像。這裏是我的剪報代碼:使用clipToMask時,圖像旋轉了90度

- (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捕獲靜止圖像:

-(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度時,我將它夾。有誰知道爲什麼以及如何解決這個問題?提前致謝。

+0

我有一個非常類似的問題,似乎無法想象它拯救了我的生活。這裏是我問的問題:http://stackoverflow.com/questions/10307521/ios-png-image-rotated-90-degrees 你最終搞清楚了嗎? – Boeckm

回答

1

這裏的替代解決方案旋轉圖片90度,它的代碼是波紋管......

yourImageView.transform = CGAffineTransformMakeRotation(3.14159265/2); 
當你想分享或保存當時的畫面

只是將圖像旋轉90°,並且如果然後需要的180度....

這是另一種解決方案,如果我有任何其他的想法的話,我會再次發佈... 希望,這幫助你...

:)