2017-05-16 2 views
0

Xamarin Forms 솔루션이 있고 iOS 프로젝트에서 스트림에있는 이미지를 UIImage로 변환하고 싶습니다. 이 코드와 함께 회전하기 위해있는 UIImage 필요Xamarin iOS에서 스트림을 UIImage로 변환하는 방법?

var _captureSession = new AVCaptureSession(); 
var _captureDevice = AVCaptureDevice.GetDefaultDevice(AVMediaType.Video); 

var output = new AVCaptureStillImageOutput { OutputSettings = new NSDictionary(AVVideo.CodecKey, AVVideo.CodecJPEG) }; 

NSError error; 
_captureSession.AddInput(new AVCaptureDeviceInput(_captureDevice, out error)); 
_captureSession.AddOutput(output); 
_captureSession.StartRunning(); 

var buffer = await output.CaptureStillImageTaskAsync(output.Connections[0]); 
NSData data = AVCaptureStillImageOutput.JpegStillToNSData(buffer); 
Stream stream = data.AsStream(); 

:

public UIImage RotateImage(UIImage image) 
{ 
    CGImage imgRef = image.CGImage; 
    float width = imgRef.Width; 
    float height = imgRef.Height; 
    CGAffineTransform transform = CGAffineTransform.MakeIdentity(); 
    RectangleF bounds = new RectangleF(0, 0, width, height); 

    float scaleRatio = bounds.Size.Width/width; 
    SizeF imageSize = new SizeF(imgRef.Width, imgRef.Height); 
    UIImageOrientation orient = image.Orientation; 
    float boundHeight; 

    if (width > height) 
    { 
     boundHeight = bounds.Size.Height; 
     bounds.Size = new SizeF(boundHeight, bounds.Size.Width); 
     transform = CGAffineTransform.MakeTranslation(imageSize.Height, 0); 
     transform = CGAffineTransform.Rotate(transform, (float)Math.PI/2.0f); 
    } 

    UIGraphics.BeginImageContext(bounds.Size); 
    CGContext context = UIGraphics.GetCurrentContext(); 

    context.ScaleCTM(-scaleRatio, scaleRatio); 
    context.TranslateCTM(-height, 0); 

    context.ConcatCTM(transform); 

    context.DrawImage(new RectangleF(0, 0, width, height), imgRef); 
    UIImage imageCopy = UIGraphics.GetImageFromCurrentImageContext(); 
    UIGraphics.EndImageContext(); 

    return imageCopy; 
} 

어떻게 스트림이있는 UIImage로 변환하는이 코드로 스트림을 얻을?

답변

0

먼저 스트림을 NSData으로 변환 한 다음 UIImage로 변환 할 수 있습니다.

var imageData = NSData.FromStream(stream); 
var image = UIImage.LoadFromData(imageData); 

모두 IDisposable 그래서 당신은 당신의 상황에 따라 using 문을 사용할 수 있습니다. using 문을 사용하지 않으면 메모리에서 이미지 개체를 수동으로 삭제해야합니다.

관련 문제