mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
* Do not depend on current device screen while calculating Image Asset size. * Scale the image asset to the exact requested size. * Process image assets natively, pass keepAspectRatio based on the stretch property and Asset options. * Fixed the splashscreen resource name as it cannot be read when containing a dot. * Updated the Image Asset scale and rotate logic based on the Native one. * Make the ImageAsset size more important than the Image decode size as its more specific. * Fixed tslint errors. * Added filePath support in the ImageAsset constructor for iOS in order to unify it with the Android implementation, support for relative files and file not found support errors. * Added unit tests for ImageAssets. * Added a sample app for UI testing of image-view with ImageAsset src. * chore: apply PR comments
73 lines
2.7 KiB
TypeScript
73 lines
2.7 KiB
TypeScript
import * as common from "./image-asset-common";
|
|
import { path as fsPath, knownFolders } from "../file-system";
|
|
|
|
global.moduleMerge(common, exports);
|
|
|
|
export class ImageAsset extends common.ImageAsset {
|
|
private _ios: PHAsset;
|
|
|
|
constructor(asset: string | PHAsset | UIImage) {
|
|
super();
|
|
if (typeof asset === "string") {
|
|
if (asset.indexOf("~/") === 0) {
|
|
asset = fsPath.join(knownFolders.currentApp().path, asset.replace("~/", ""));
|
|
}
|
|
|
|
this.nativeImage = UIImage.imageWithContentsOfFile(asset);
|
|
}
|
|
else if (asset instanceof UIImage) {
|
|
this.nativeImage = asset
|
|
}
|
|
else {
|
|
this.ios = asset;
|
|
}
|
|
}
|
|
|
|
get ios(): PHAsset {
|
|
return this._ios;
|
|
}
|
|
|
|
set ios(value: PHAsset) {
|
|
this._ios = value;
|
|
}
|
|
|
|
public getImageAsync(callback: (image, error) => void) {
|
|
if (!this.ios && !this.nativeImage) {
|
|
callback(null, "Asset cannot be found.");
|
|
}
|
|
|
|
let srcWidth = this.nativeImage ? this.nativeImage.size.width : this.ios.pixelWidth;
|
|
let srcHeight = this.nativeImage ? this.nativeImage.size.height : this.ios.pixelHeight;
|
|
let requestedSize = common.getRequestedImageSize({ width: srcWidth, height: srcHeight }, this.options);
|
|
|
|
if (this.nativeImage) {
|
|
let newSize = CGSizeMake(requestedSize.width, requestedSize.height);
|
|
let resizedImage = this.scaleImage(this.nativeImage, newSize);
|
|
callback(resizedImage, null);
|
|
return;
|
|
}
|
|
|
|
let imageRequestOptions = PHImageRequestOptions.alloc().init();
|
|
imageRequestOptions.deliveryMode = PHImageRequestOptionsDeliveryMode.HighQualityFormat;
|
|
|
|
PHImageManager.defaultManager().requestImageForAssetTargetSizeContentModeOptionsResultHandler(this.ios, requestedSize, PHImageContentMode.AspectFit, imageRequestOptions,
|
|
(image, imageResultInfo) => {
|
|
if (image) {
|
|
let resultImage = this.scaleImage(image, requestedSize);
|
|
callback(resultImage, null);
|
|
}
|
|
else {
|
|
callback(null, imageResultInfo.valueForKey(PHImageErrorKey));
|
|
}
|
|
}
|
|
);
|
|
}
|
|
|
|
private scaleImage(image: UIImage, requestedSize: {width: number, height: number}): UIImage {
|
|
UIGraphicsBeginImageContextWithOptions(requestedSize, false, 0.0);
|
|
image.drawInRect(CGRectMake(0, 0, requestedSize.width, requestedSize.height));
|
|
let resultImage = UIGraphicsGetImageFromCurrentImageContext();
|
|
UIGraphicsEndImageContext();
|
|
return resultImage;
|
|
}
|
|
} |