Ejemplo n.º 1
0
        internal static async Task <StorageItemThumbnail> CreatePhotoThumbnailAsync(StorageFile file)
        {
#if __MAC__
            NSImage image = NSImage.FromStream(await file.OpenStreamForReadAsync());
            double  ratio = image.Size.Width / image.Size.Height;

            NSImage newImage = new NSImage(new CGSize(240, 240 * ratio));
            newImage.LockFocus();
            image.Size = newImage.Size;

            image.Draw(new CGPoint(0, 0), new CGRect(0, 0, newImage.Size.Width, newImage.Size.Height), NSCompositingOperation.Copy, 1.0f);
            newImage.UnlockFocus();

            NSMutableData      buffer = new NSMutableData();
            CGImageDestination dest   = CGImageDestination.Create(buffer, UTType.JPEG, 1);
            dest.AddImage(newImage.CGImage);

            return(new StorageItemThumbnail(buffer.AsStream()));
#else
            UIImage image = UIImage.FromFile(file.Path);

            UIImage image2 = image.Scale(new CGSize(240, 240));
            image.Dispose();

            return(new StorageItemThumbnail(image2.AsJPEG().AsStream()));
#endif
        }
Ejemplo n.º 2
0
        public void SaveTo(Stream stream, CompressionFormat compression)
        {
            var data = new NSMutableData();
            CGImageDestination dest = null;

            switch (compression)
            {
            case CompressionFormat.Jpeg:
                dest = CGImageDestination.Create(data, MobileCoreServices.UTType.JPEG, imageCount: 1);
                break;

            case CompressionFormat.Png:
                dest = CGImageDestination.Create(data, MobileCoreServices.UTType.PNG, imageCount: 1);
                break;
            }
            if (dest != null)
            {
                dest.AddImage(cgImage);
                dest.Close();
                using (var bitmapStream = data.AsStream()) {
                    bitmapStream.CopyTo(stream);
                }
                data.Dispose();
            }
        }
Ejemplo n.º 3
0
        internal static async Task <StorageItemThumbnail> CreateVideoThumbnailAsync(StorageFile file)
        {
            AVAsset asset = AVUrlAsset.FromUrl(NSUrl.FromFilename(file.Path));
            AVAssetImageGenerator generator = AVAssetImageGenerator.FromAsset(asset);

            generator.AppliesPreferredTrackTransform = true;
            NSError error;
            CMTime  actualTime;
            CMTime  time  = CMTime.FromSeconds(asset.Duration.Seconds / 2, asset.Duration.TimeScale);
            CGImage image = generator.CopyCGImageAtTime(time, out actualTime, out error);

#if __MAC__
            NSMutableData      buffer = new NSMutableData();
            CGImageDestination dest   = CGImageDestination.Create(buffer, UTType.JPEG, 1, null);
            dest.AddImage(image);
            return(new StorageItemThumbnail(buffer.AsStream()));
#else
            UIImage image2 = UIImage.FromImage(image);
            image.Dispose();

            UIImage image3 = image2.Scale(new CGSize(240, 240));
            image2.Dispose();

            return(new StorageItemThumbnail(image3.AsJPEG().AsStream()));
#endif
        }
Ejemplo n.º 4
0
        public override void WriteToStream(Stream stream)
        {
            Close();

            if (_data != null)
            {
                using (var inputStream = _data.AsStream())
                {
                    inputStream.CopyTo(stream);
                }
            }
        }
Ejemplo n.º 5
0
        public new void Save(Stream stream, ImageFormat format)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            using (var imageData = new NSMutableData())
            {
                using (var dest = CGImageDestination.Create(imageData, GetTypeIdentifier(format), frameCount))
                    Save(dest);

                using (var ms = imageData.AsStream())
                    ms.CopyTo(stream);
            }
        }
        private async Task <OperationResult <MediaModel> > UploadPhoto(UIImage photo, NSDictionary metadata)
        {
            Stream stream = null;

            try
            {
                var compression    = 1f;
                var maxCompression = 0.1f;
                int maxFileSize    = _photoSize * 1024;

                var byteArray = photo.AsJPEG(compression);

                while (byteArray.Count() > maxFileSize && compression > maxCompression)
                {
                    compression -= 0.1f;
                    byteArray    = photo.AsJPEG(compression);
                }

                if (metadata != null)
                {
                    //exif setup
                    var editedExifData       = RemakeMetadata(metadata, photo);
                    var newImageDataWithExif = new NSMutableData();
                    var imageDestination     = CGImageDestination.Create(newImageDataWithExif, "public.jpeg", 0);
                    imageDestination.AddImage(new UIImage(byteArray).CGImage, editedExifData);
                    imageDestination.Close();
                    stream = newImageDataWithExif.AsStream();
                }
                else
                {
                    stream = byteArray.AsStream();
                }

                var request = new UploadMediaModel(AppSettings.User.UserInfo, stream, ImageExtension);
                return(await _presenter.TryUploadMedia(request));
            }
            catch (Exception ex)
            {
                AppSettings.Reporter.SendCrash(ex);
                return(new OperationResult <MediaModel>(new AppError(LocalizationKeys.PhotoProcessingError)));
            }
            finally
            {
                stream?.Flush();
                stream?.Dispose();
            }
        }
Ejemplo n.º 7
0
        public void SaveAsPng(Stream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException();
            }

            using (var data = new NSMutableData()) {
                using (var dest = CGImageDestination.Create(data, "public.png", 1)) {
                    if (dest == null)
                    {
                        throw new InvalidOperationException(string.Format("Could not create image destination from {0}.", stream));
                    }
                    dest.AddImage(image);
                    dest.Close();
                }
                data.AsStream().CopyTo(stream);
            }
        }
Ejemplo n.º 8
0
 public override void FinishedLoading(NSUrlConnection connection)
 {
     callback(buffer.AsStream(), null);
 }
Ejemplo n.º 9
0
        public void SaveAsPng(Stream stream)
        {
            if (stream == null)
                throw new ArgumentNullException ();

            using (var data = new NSMutableData ()) {
                using (var dest = CGImageDestination.Create (data, "public.png", 1)) {
                    if (dest == null) {
                        throw new InvalidOperationException (string.Format ("Could not create image destination from {0}.", stream));
                    }
                    dest.AddImage (image);
                    dest.Close ();
                }
                data.AsStream ().CopyTo (stream);
            }
        }
Ejemplo n.º 10
0
        private async Task <OperationResult <MediaModel> > UploadPhoto(UIImage photo, NSDictionary metadata)
        {
            Stream stream = null;

            try
            {
                var compression    = 1f;
                var maxCompression = 0.1f;
                int maxFileSize    = _photoSize * 1024;

                var byteArray = photo.AsJPEG(compression);

                while (byteArray.Count() > maxFileSize && compression > maxCompression)
                {
                    compression -= 0.1f;
                    byteArray    = photo.AsJPEG(compression);
                }

                if (metadata != null)
                {
                    //exif setup
                    var editedExifData       = RemakeMetadata(metadata, photo);
                    var newImageDataWithExif = new NSMutableData();
                    var imageDestination     = CGImageDestination.Create(newImageDataWithExif, "public.jpeg", 0);
                    imageDestination.AddImage(new UIImage(byteArray).CGImage, editedExifData);
                    imageDestination.Close();
                    stream = newImageDataWithExif.AsStream();
                }
                else
                {
                    stream = byteArray.AsStream();
                }

                var request      = new UploadMediaModel(AppSettings.User.UserInfo, stream, ImageExtension);
                var serverResult = await _presenter.TryUploadMedia(request);

                if (!serverResult.IsSuccess)
                {
                    return(new OperationResult <MediaModel>(serverResult.Exception));
                }

                var uuidModel = serverResult.Result;
                var done      = false;
                do
                {
                    var state = await _presenter.TryGetMediaStatus(uuidModel);

                    if (state.IsSuccess)
                    {
                        switch (state.Result.Code)
                        {
                        case UploadMediaCode.Done:
                            done = true;
                            break;

                        case UploadMediaCode.FailedToProcess:
                        case UploadMediaCode.FailedToUpload:
                        case UploadMediaCode.FailedToSave:
                            return(new OperationResult <MediaModel>(new Exception(state.Result.Message)));

                        default:
                            await Task.Delay(3000);

                            break;
                        }
                    }
                } while (!done);

                return(await _presenter.TryGetMediaResult(uuidModel));
            }
            catch (Exception ex)
            {
                return(new OperationResult <MediaModel>(new InternalException(LocalizationKeys.PhotoProcessingError, ex)));
            }
            finally
            {
                stream?.Flush();
                stream?.Dispose();
            }
        }