Пример #1
0
        IObservable<Unit> SaveThumbnailToFile (FileRequest request)
        {
            return Observable.Create<Unit> (o => {
                var description = request.DescriptionAs<AssetDescription> ();
                var disp = new CancellationDisposable ();
                var token = disp.Token;

                GetAsset (description, token).ContinueWith (t => {
                    var saveUrl = NSUrl.FromFilename (request.Filename);
                    
                    using (var asset = t.Result)
                    using (var representation = asset.DefaultRepresentation)
                    using (var thumbnail = new UIImage (asset.Thumbnail))
                    using (var destination = CGImageDestination.FromUrl (saveUrl, representation.Uti, 1))
                    using (var cgImage = thumbnail.CGImage) {
                        destination.AddImage (cgImage, new NSMutableDictionary {
                            { CGImageProperties.Orientation, NSNumber.FromInt32 (ImageHelper.ToExifOrientation (thumbnail.Orientation)) }
                        });
                        
                        destination.Close ();
                    }
                    
                    o.OnCompleted ();
                }, token).RouteExceptions (o);
                
                return disp;
            });
        }
Пример #2
0
        static Task StartDropboxTask (DBRestClient client, FileRequest request, CancellationToken token)
        {
            var description = request.DescriptionAs<DropboxDescription> ();
            var path = description.Path;
            var filename = System.IO.Path.GetFullPath (request.Filename);

            switch (description.Kind) {
            case DropboxDescription.DropboxImageKind.LargeThumbnail:
                return client.LoadThumbnailTask (path, "large", filename, token);
            case DropboxDescription.DropboxImageKind.FullResolution:
                return client.LoadFileTask (path, filename, token);
            default:
                throw new NotImplementedException ();
            }
        }
Пример #3
0
        IObservable<Unit> SaveFullResolutionImageToFile (FileRequest request)
        {
            return Observable.Create<Unit> (o => {
                var description = request.DescriptionAs<AssetDescription> ();
                var disp = new CancellationDisposable ();
                var token = disp.Token;

                GetAsset (description, token).ContinueWith (t => {
                    using (File.Create (request.Filename))
                    using (var asset = t.Result)
                    using (var representation = asset.DefaultRepresentation) 
                    using (var stream = new NSOutputStream (request.Filename, true)) {
                        stream.Open ();

                        long offset = 0;
                        uint bytesRead = 0;

                        NSError err;

                        // A large enough buffer that shouldn't cause memory warnings
                        byte [] buffer = new byte [131072];

                        GCHandle handle = GCHandle.Alloc (buffer, GCHandleType.Pinned);
                        IntPtr pointer = handle.AddrOfPinnedObject ();
                        
                        unsafe {
                            while (offset < representation.Size && stream.HasSpaceAvailable ()) {
                                bytesRead = representation.GetBytes (pointer, offset, (uint)buffer.Length, out err);
                                stream.Write (buffer, bytesRead);
                                offset += bytesRead;
                            }
                        }
                        
                        stream.Close ();
                        handle.Free ();
                    }

                    o.OnCompleted ();
                }, token).RouteExceptions (o);

                return disp;
            });
        }