thinktecture / boardz-cross-platform-sample

Sample application for demonstrating various aspects of modern cross-platform applications - using Angular 2, Cordova, Electron & gulp. With a .NET Web API & SignalR backend.
MIT License
78 stars 30 forks source link

Optimize using and uploading images in Cordova #42

Open ChristianWeyer opened 8 years ago

ChristianWeyer commented 8 years ago

By using the standard e.g. iPhone camera we get very large photos with the current code. It can tkae ages to upload and ages to download all the photos.

We need to optimize this e.g. on Cordova.

We could create thumbnails on the server as well.

ChristianWeyer commented 8 years ago

We should use the File API in Cordova - something like this: var onCordovaDeviceReady = () => { const camera = window.navigator.camera;

            var options = {
                quality: 20,
                targetWidth: 400,
                destinationType: camera.DestinationType.FILE_URI,
                sourceType: camera.PictureSourceType.CAMERA,
                encodingType: camera.EncodingType.PNG,
                saveToPhotoAlbum: false,
                correctOrientation: true
            };

            camera.getPicture(imageUri => {
                window.resolveLocalFileSystemURL(imageUri, function (fileEntry) {
                    var imageFileUrl = fileEntry.nativeURL;
                    observer.next(imageFileUrl);
                    removeDomListener();
                    observer.complete();
                });
            }, error => {
                observer.error(error);
                removeDomListener();
                observer.complete();
            }, options);
        };

Then we should post the data as multipart mime. There is a sample how to implement this in my old sample app.

Server/Web API: https://github.com/ChristianWeyer/myProducts-End-to-End/blob/master/src/myProducts.Services/Controllers/ArticlesController.cs#L114

Client/ng1: https://github.com/ChristianWeyer/myProducts-End-to-End/blob/master/src/myProducts.Web/client/app/services/articlesService.js#L54