2014-01-17 139 views
-1

目前我正在開發基於相機的應用程序。我使用UIImageView內的相機圖像來更容易操作。現在我想從核心成像添加一些效果。我能以某種方式從視圖中獲得圖像嗎?從UIImageView獲取UIImage

這是我如何添加效果,但它只適用於圖像。

NSString *filePath = 
[[NSBundle mainBundle] pathForResource:@"IpadIcon" ofType:@"png"]; 
NSURL *fileNameAndPath = [NSURL fileURLWithPath:filePath]; 

CIImage *beginImage = 
[CIImage imageWithContentsOfURL:fileNameAndPath]; 
CIContext *context = [CIContext contextWithOptions:nil]; 

CIFilter *filter = [CIFilter filterWithName:@"CISepiaTone" keysAndValues: kCIInputImageKey, beginImage, @"inputIntensity", [NSNumber numberWithFloat:0.8], nil]; 
CIImage *outputImage = [filter outputImage]; 

CGImageRef cgimg = 
[context createCGImage:outputImage fromRect:[outputImage extent]]; 
UIImage *newImg = [UIImage imageWithCGImage:cgimg]; 

[captureImage setImage:newImg]; 

獲取圖像

- (void) capImage { //method to capture image from AVCaptureSession video feed 
    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; 
     } 
    } 


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

     if (imageSampleBuffer != NULL) { 
      NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer]; 
      [self processImage:[UIImage imageWithData:imageData]]; 
     } 
    }]; 
} 
+0

你能具體談談您遇到的問題? –

+0

我有一個UIImageView內的圖片。現在我需要把它弄出來。可能嗎 ? – WildWorld

+1

'UIImage * theImage = imageView.image;'? – hgwhittle

回答

1

使用的UIImageView的image屬性獲取圖像:

UIImage *theImage = imageView.image;

+0

謝謝:)在冷靜後投票 – WildWorld