在 Ionic 3 中显示 Native Camera 拍摄的 FILE_URI 图像
Displaying a FILE_URI image taken by Native Camera in Ionic 3
如何在 Ionic 3 中显示用户使用 @ionic-native/camera
拍摄的 FILE_URI 图像?
我可以使用 Ionic Native 的相机获取 FILE_URI 图像 URL,结果如下:
file:///var/mobile/Containers/Data/Application/XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXX/tmp/cdv_photo_005.jpg
但是,当我尝试通过引用我的视图模板中的 URI 向用户显示该图像时,该图像从未加载。
我尝试过的事情:
-在视图中直接使用图片URI
<img src="{{profile.image}}"> // Never loads
-通过在页面组件中包含 DomSanitizer
来净化 URI:
<img [src]="domSanitizer.bypassSecurityTrustUrl(profile.image)"> // Never loads
由于性能拖累,我宁愿不使用 base64 图像。我在这里做错了什么?
您好尝试使用文件路径离子插件
path = "file:///var/mobile/Containers/Data/Application/XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXX/tmp/cdv_photo_005.jpg";
this.filePath.resolveNativePath(path)
.then(filePath => console.log(filePath))
.catch(err => console.log(err));
从 'ionic-angular' 导入 { normalizeURL };
<img *ngIf="base64Image" src="{{base64Image}}"/>
openCamera(pictureSourceType: any) {
let options: CameraOptions = {
quality: 95,
destinationType: this.camera.DestinationType.FILE_URI,
sourceType: pictureSourceType,
encodingType: this.camera.EncodingType.PNG,
targetWidth: 400,
targetHeight: 400,
saveToPhotoAlbum: true,
correctOrientation: true
};
this.camera.getPicture(options).then(imageData => {
if (this.platform.is('ios')) {
this.base64Image = normalizeURL(imageData);
// Alternatively if the problem only occurs in ios and normalizeURL
// doesn't work for you then you can also use:
// this.base64Image= imageData.replace(/^file:\/\//, '');
}
else {
this.base64Image= "data:image/jpeg;base64," + imageData;
}
}, error => {
console.log('ERROR -> ' + JSON.stringify(error));
});
}
您可以使用下面的代码来显示图像
savePhotoClick = () =>{
const options: CameraOptions = {
quality: 70,
destinationType: this.camera.DestinationType.DATA_URL,
encodingType: this.camera.EncodingType.JPEG,
mediaType: this.camera.MediaType.PICTURE
}
this.camera.getPicture(options).then((imageData) => {
// imageData is either a base64 encoded string or a file URI
// If it's base64 (DATA_URL):
this.base64Image = 'data:image/jpeg;base64,' + imageData;
}, (err) => {
// Handle error
});
然后使用img标签进行展示
<img [src]="base64Image" *ngIf="base64Image" />
如何在 Ionic 3 中显示用户使用 @ionic-native/camera
拍摄的 FILE_URI 图像?
我可以使用 Ionic Native 的相机获取 FILE_URI 图像 URL,结果如下:
file:///var/mobile/Containers/Data/Application/XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXX/tmp/cdv_photo_005.jpg
但是,当我尝试通过引用我的视图模板中的 URI 向用户显示该图像时,该图像从未加载。
我尝试过的事情:
-在视图中直接使用图片URI
<img src="{{profile.image}}"> // Never loads
-通过在页面组件中包含 DomSanitizer
来净化 URI:
<img [src]="domSanitizer.bypassSecurityTrustUrl(profile.image)"> // Never loads
由于性能拖累,我宁愿不使用 base64 图像。我在这里做错了什么?
您好尝试使用文件路径离子插件
path = "file:///var/mobile/Containers/Data/Application/XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXX/tmp/cdv_photo_005.jpg";
this.filePath.resolveNativePath(path)
.then(filePath => console.log(filePath))
.catch(err => console.log(err));
从 'ionic-angular' 导入 { normalizeURL };
<img *ngIf="base64Image" src="{{base64Image}}"/>
openCamera(pictureSourceType: any) {
let options: CameraOptions = {
quality: 95,
destinationType: this.camera.DestinationType.FILE_URI,
sourceType: pictureSourceType,
encodingType: this.camera.EncodingType.PNG,
targetWidth: 400,
targetHeight: 400,
saveToPhotoAlbum: true,
correctOrientation: true
};
this.camera.getPicture(options).then(imageData => {
if (this.platform.is('ios')) {
this.base64Image = normalizeURL(imageData);
// Alternatively if the problem only occurs in ios and normalizeURL
// doesn't work for you then you can also use:
// this.base64Image= imageData.replace(/^file:\/\//, '');
}
else {
this.base64Image= "data:image/jpeg;base64," + imageData;
}
}, error => {
console.log('ERROR -> ' + JSON.stringify(error));
});
}
您可以使用下面的代码来显示图像
savePhotoClick = () =>{
const options: CameraOptions = {
quality: 70,
destinationType: this.camera.DestinationType.DATA_URL,
encodingType: this.camera.EncodingType.JPEG,
mediaType: this.camera.MediaType.PICTURE
}
this.camera.getPicture(options).then((imageData) => {
// imageData is either a base64 encoded string or a file URI
// If it's base64 (DATA_URL):
this.base64Image = 'data:image/jpeg;base64,' + imageData;
}, (err) => {
// Handle error
});
然后使用img标签进行展示
<img [src]="base64Image" *ngIf="base64Image" />