Ionic framework ionic 3中共享图像时崩溃

Ionic framework ionic 3中共享图像时崩溃,ionic-framework,ionic3,Ionic Framework,Ionic3,我已经尝试了我的爱奥尼亚应用程序的摄像头插件,它的工作成功。当我试图与社交分享分享时,它崩溃并关闭了。 请查找代码以更正我的错误 注: 如果该Base64图像中的相机图片未超过,则共享工作正常。如果图片存在崩溃,那么我认为错误可能与Base64图像有关,我无法指出错误,让我帮助 home.ts文件 从'@angular/core'导入{Component}; 从'ionic angular'导入{NavController}; 从'@ionic native/Camera'导入{Camera,C

我已经尝试了我的爱奥尼亚应用程序的摄像头插件,它的工作成功。当我试图与社交分享分享时,它崩溃并关闭了。 请查找代码以更正我的错误

注: 如果该Base64图像中的相机图片未超过,则共享工作正常。如果图片存在崩溃,那么我认为错误可能与Base64图像有关,我无法指出错误,让我帮助

home.ts文件
从'@angular/core'导入{Component};
从'ionic angular'导入{NavController};
从'@ionic native/Camera'导入{Camera,CameraOptions};
从'@ionic native/social Share'导入{SocialShareing};
@组成部分({
选择器:“主页”,
templateUrl:'home.html'
})
导出类主页{
public base64Image:字符串;
构造函数(公共navCtrl:NavController,专用摄影机:摄影机,专用社交共享:社交共享){
}
opencamera()
{
常量选项:摄影机选项={
质量:100,
destinationType:this.camera.destinationType.DATA\u URL,
编码类型:this.camera.encodingType.JPEG,
mediaType:this.camera.mediaType.PICTURE
}
this.camera.getPicture(选项)。然后((图像数据)=>{
//imageData是base64编码的字符串或文件URI
//如果是base64:
this.base64Image='data:image/jpeg;base64'+imageData;
},(错误)=>{
//处理错误
});
console.log('点击摄像头按钮');
}
分享
{
这个.socialSharing.canShareViaEmail()。然后(()=>{
//可以通过电子邮件进行共享
console.log('sharing is successfull');
}).catch(()=>{
//无法通过电子邮件共享
});
//通过电子邮件分享
this.socialSharing.shareViaWhatsAppToReceiver('98947XXXXX','checking sample','this.base64Image',null)
.然后(()=>{
//成功!
console.log('shared');
}).catch(()=>{
//错误!
});
}
}
home.html文件

家
照相机
最新图片:
分享
切换菜单
正如您可以在中看到的:

数据\u URL可能占用大量内存,导致应用程序崩溃或内存不足错误。如果可能,请使用文件URI或本机URI

这是我通常用来拍照和分享照片的代码的一部分。共享部分与您的部分并不完全相同,但您可以修改它以适应您的需求

组件

// Angular
import { Component, NgZone } from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser';

// Ionic
import { Platform } from 'ionic-angular';

// Cordova plugins
import { Camera, CameraOptions } from '@ionic-native/camera';
import { SocialSharing } from '@ionic-native/social-sharing';

@Component({ 
    selector: 'page-share', 
    templateUrl: 'share.html' 
})
export class SharePage {

    public imageUrl: string;
    public imageUrlToShare: string;

    constructor(private platform: Platform,
                private ngZone: NgZone,
                private domSanitizer: DomSanitizer,
                private camera: Camera,
                private socialSharing: SocialSharing) {}

    public onTakePictureClick(): void {
        const options: CameraOptions = {
            quality: 100,
            destinationType: this.camera.DestinationType.FILE_URI, // <-- User FILE_URI to prevent the memory error :)
            correctOrientation: true,
            encodingType: this.camera.EncodingType.JPEG,
            mediaType: this.camera.MediaType.PICTURE
        }

        // Use the plugin to get the photo from the camera
        this.getPhotoWithOptions(options);
    }

    private getPhotoWithOptions(options: CameraOptions): void {
        this.camera.getPicture(options).then((imageData) => {
            this.getPhotoUrl(imageData);
        }, (err) => {
            // Handle the error!!
            console.log(err);
        });
    }

    private getPhotoUrl(uri: string): void {
        if (!uri) { return; }

        // iOS fix for getting the proper url
        uri = this.platform.is('ios') && uri.indexOf('file://') < 0 ? `file://${uri}` : uri;

        (<any>window).resolveLocalFileSystemURL(uri, entry => {
            this.ngZone.run(() => {

                // Use this property to show the image on the view
                this.imageUrl = entry.toInternalURL();

                // Use this property to share the image using the SocialSharing plugin
                this.imageUrlToShare = entry.toURL();

            });
        });
    }

    public onShareClick(): void {
        const options = {
            message: null, // not supported on some apps (Facebook, Instagram)
            subject: null, // fi. for email
            files: [this.imageUrlToShare] // an array of filenames either locally or remotely
        };

        this.socialSharing.shareWithOptions(options)
            .catch(error => {
                // Handle the error!!
                console.log(err);
            })

    }

    public getTrustResourceUrl(url: string) {
        return this.domSanitizer.bypassSecurityTrustResourceUrl(url);
    }
}
//角度
从“@angular/core”导入{Component,NgZone};
从“@angular/platform browser”导入{domsanizer};
//离子的
从“离子角度”导入{Platform};
//Cordova插件
从'@ionic native/Camera'导入{Camera,CameraOptions};
从'@ionic native/social Share'导入{SocialShareing};
@组件({
选择器:“页面共享”,
templateUrl:'share.html'
})
导出类共享页{
publicmageurl:string;
公共imageUrlToShare:字符串;
建造商(专用平台:平台、,
私人ngZone:ngZone,
私有Domsanizer:Domsanizer,
私人摄像机:摄像机,
私人社会共享:社会共享{}
public onTakePictureClick():void{
常量选项:摄影机选项={
质量:100,
destinationType:this.camera.destinationType.FILE\u URI,//{
这个.getPhotoUrl(imageData);
},(错误)=>{
//处理错误!!
控制台日志(err);
});
}
私有getPhotoUrl(uri:string):void{
如果(!uri){return;}
//iOS修复程序,用于获取正确的url
uri=this.platform.is('ios')&&uri.indexOf('file://'))<0?`file://${uri}`:uri;
(窗口)。resolveLocalFileSystemURL(uri,条目=>{
此.ngZone.run(()=>{
//使用此属性可在视图上显示图像
this.imageUrl=entry.toInternalURL();
//使用此属性可使用SocialShareing插件共享图像
this.imageUrlToShare=entry.toURL();
});
});
}
public onShareClick():void{
常量选项={
消息:null,//某些应用程序(Facebook、Instagram)不支持
主题:null,//fi.用于电子邮件
files:[this.imageUrlToShare]//本地或远程文件名数组
};
this.socialSharing.shareWithOptions(选项)
.catch(错误=>{
//处理错误!!
控制台日志(err);
})
}
公共getTrustResourceUrl(url:string){
返回此.domsanizer.bypassSecurityTrustResourceUrl(url);
}
}
查看

<ion-header>
    <ion-navbar color="primary">
        <ion-title>Share</ion-title>
    </ion-navbar>
</ion-header>

<ion-content padding>

    <div *ngIf="imageUrl" class="image-container">
        <img class="image" [src]="getTrustResourceUrl(imageUrl)" alt="Picture">
    </div>

    <button (click)="onTakePictureClick()" ion-button block icon-left>
        <ion-icon name="camera"></ion-icon>Take picture
    </button>

    <button *ngIf="imageUrl" (click)="onShareClick()" ion-button block icon-left>
        <ion-icon name="share-alt"></ion-icon>Share
    </button>

</ion-content>

分享
拍照
分享