public void GetProperties() { #if MONOMAC using (var imageSource = CGImageSource.FromUrl(fileUrl)) { #else using (var imageSource = CGImageSource.FromUrl(NSUrl.FromFilename(filename))) { #endif CGImageOptions options = new CGImageOptions() { ShouldCache = false }; var props = imageSource.GetProperties(options); Assert.Null(props.PixelWidth, "PixelHeight-0"); Assert.Null(props.PixelHeight, "PixelWidth-0"); // image is "optimized" for devices (and a lot bigger at 10351 bytes ;-) Assert.That(props.FileSize, Is.AtLeast(7318), "FileSize"); props = imageSource.GetProperties(0, options); Assert.AreEqual(57, props.PixelWidth, "PixelHeight"); Assert.AreEqual(57, props.PixelHeight, "PixelWidth"); Assert.AreEqual(CGImageColorModel.RGB, props.ColorModel, "ColorModel"); Assert.AreEqual(8, props.Depth, "Depth"); } }
public object GetImageFromUri(FileUri imageUri, int targetWidth, int targetHeight, int rotation) { using (Stream stream = GetFileSystem().OpenFile(imageUri, UniversalFileMode.Open, UniversalFileAccess.Read, UniversalFileShare.Read)) { CGImageSource source = CGImageSource.FromUrl(imageUri.ToNSUrl()); CGImageOptions options = new CGImageOptions(); options.ShouldCache = false; var props = source.CopyProperties(options, 0); int imageWidth = ((NSNumber)props["PixelWidth"]).Int32Value; int imageHeight = ((NSNumber)props["PixelHeight"]).Int32Value; int scale = 1; while (imageWidth / scale / 2 >= (nfloat)targetWidth && imageHeight / scale / 2 >= (nfloat)targetHeight) { scale *= 2; } stream.Seek(0, SeekOrigin.Begin); UIImage image = UIImage.LoadFromData(NSData.FromUrl(imageUri.ToNSUrl()), scale); if (rotation != 0) { float radians = rotation * (float)Math.PI / 180f; CGRect imageFrame = new CGRect(0, 0, image.Size.Width, image.Size.Height); CGAffineTransform t = CGAffineTransform.MakeRotation(radians); imageFrame = t.TransformRect(imageFrame); CGSize rotatedSize = new CGSize(imageFrame.Width, imageFrame.Height); UIGraphics.BeginImageContext(rotatedSize); using (CGContext context = UIGraphics.GetCurrentContext()) { context.TranslateCTM(rotatedSize.Width / 2, rotatedSize.Height / 2); context.RotateCTM(-radians); context.ScaleCTM(1.0f, -1.0f); image.Draw(new CGRect(-image.Size.Width / 2, -image.Size.Height / 2, image.Size.Width, image.Size.Height)); image = UIGraphics.GetImageFromCurrentImageContext(); } UIGraphics.EndImageContext(); } return(image); } }
public ImageSize ImageSizeIos(string localPath) { using (var src = CGImageSource.FromUrl(NSUrl.FromFilename(localPath))) { CGImageOptions options = new CGImageOptions() { ShouldCache = false }; // do not forget the '0' image index or you won't get what you're looking for! var p = src.GetProperties(0, options); return(new ImageSize((int)p.PixelWidth, (int)p.PixelHeight)); } }
public void CopyMetadata() { TestRuntime.AssertXcodeVersion(5, 0); using (var imageSource = CGImageSource.FromUrl(NSUrl.FromFilename(filename))) { CGImageOptions options = new CGImageOptions() { ShouldCacheImmediately = true }; using (CGImageMetadata metadata = imageSource.CopyMetadata(0, options)) { Console.WriteLine(); } } }
private CGImage CreateImage(CMSampleBuffer sampleBuffer) { CGImage image = null; CMFormatDescription formatDescription = sampleBuffer.GetFormatDescription(); var subType = formatDescription.MediaSubType; CMBlockBuffer blockBuffer = sampleBuffer.GetDataBuffer(); if (blockBuffer != null) { if (subType != (int)CMVideoCodecType.JPEG) { throw new Exception("Block buffer must be JPEG encoded."); } var jpegData = new NSMutableData(); jpegData.Length = blockBuffer.DataLength; blockBuffer.CopyDataBytes(0, blockBuffer.DataLength, jpegData.Bytes); using (var imageSource = CGImageSource.FromData(jpegData)) { var decodeOptions = new CGImageOptions { ShouldAllowFloat = false, ShouldCache = false }; image = imageSource.CreateImage(0, decodeOptions); } } else { if (subType != (int)CVPixelFormatType.CV32BGRA) { throw new Exception("Image buffer must be BGRA encoded."); } CVImageBuffer imageBuffer = sampleBuffer.GetImageBuffer(); using (var colorSpace = CGColorSpace.CreateDeviceRGB()) using (var bitmapContext = new CGBitmapContext(imageBuffer.Handle, (int)imageBuffer.DisplaySize.Width, (int)imageBuffer.DisplaySize.Height, 8, 0, colorSpace, CGImageAlphaInfo.NoneSkipFirst)) { image = bitmapContext.ToImage(); } } return(image); }
public void CopyMetadata() { if (!TestRuntime.CheckSystemAndSDKVersion(7, 0)) { Assert.Ignore("Only on iOS7+"); } using (var imageSource = CGImageSource.FromUrl(NSUrl.FromFilename(filename))) { CGImageOptions options = new CGImageOptions() { ShouldCacheImmediately = true }; using (CGImageMetadata metadata = imageSource.CopyMetadata(0, options)) { Console.WriteLine(); } } }
private CGImage CreateImage (CMSampleBuffer sampleBuffer) { CGImage image = null; CMFormatDescription formatDescription = sampleBuffer.GetFormatDescription (); var subType = formatDescription.MediaSubType; CMBlockBuffer blockBuffer = sampleBuffer.GetDataBuffer (); if (blockBuffer != null) { if (subType != (int)CMVideoCodecType.JPEG) throw new Exception ("Block buffer must be JPEG encoded."); var jpegData = new NSMutableData (); jpegData.Length = blockBuffer.DataLength; blockBuffer.CopyDataBytes (0, blockBuffer.DataLength, jpegData.Bytes); using (var imageSource = CGImageSource.FromData (jpegData)) { var decodeOptions = new CGImageOptions { ShouldAllowFloat = false, ShouldCache = false }; image = imageSource.CreateImage (0, decodeOptions); } } else { if (subType != (int)CVPixelFormatType.CV32BGRA) throw new Exception ("Image buffer must be BGRA encoded."); CVImageBuffer imageBuffer = sampleBuffer.GetImageBuffer (); using (var colorSpace = CGColorSpace.CreateDeviceRGB ()) using (var bitmapContext = new CGBitmapContext (imageBuffer.Handle, (int)imageBuffer.DisplaySize.Width, (int)imageBuffer.DisplaySize.Height, 8, 0, colorSpace, CGImageAlphaInfo.NoneSkipFirst)) { image = bitmapContext.ToImage (); } } return image; }