mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
Fixed the memory leaks in iOS.
This commit is contained in:
@@ -7,7 +7,7 @@
|
|||||||
<ListView.itemTemplate>
|
<ListView.itemTemplate>
|
||||||
<!-- Binding in template property of an component will use the bindingContext provided by the component. -->
|
<!-- Binding in template property of an component will use the bindingContext provided by the component. -->
|
||||||
<GridLayout columns="auto, *, auto" rows="auto, 25">
|
<GridLayout columns="auto, *, auto" rows="auto, 25">
|
||||||
<Image imageSource="{{ thumbnailImageSource || defaultThumbnailImageSource }}" cssClass="thumbnail" rowSpan="2"/>
|
<Image src="{{ thumbnailImage }}" cssClass="thumbnail" rowSpan="2"/>
|
||||||
<Label text="{{ title || 'Downloading...' }}" textWrap="true" cssClass="title" col="1" colSpan="2" minHeight="50" />
|
<Label text="{{ title || 'Downloading...' }}" textWrap="true" cssClass="title" col="1" colSpan="2" minHeight="50" />
|
||||||
<Label text="{{ author ? 'by ' + author : '' }}" cssClass="author" col="1" row="1" />
|
<Label text="{{ author ? 'by ' + author : '' }}" cssClass="author" col="1" row="1" />
|
||||||
<Label text="{{ num_comments ? num_comments + ' comments' : '' }}" cssClass="comments" col="2" row="1" />
|
<Label text="{{ num_comments ? num_comments + ' comments' : '' }}" cssClass="comments" col="2" row="1" />
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ var firstThumbnailImageSource = imageSource.fromFile("~/res/first-image.png");
|
|||||||
var defaultImageSource = imageSource.fromFile("~/res/reddit-logo-transparent.png");
|
var defaultImageSource = imageSource.fromFile("~/res/reddit-logo-transparent.png");
|
||||||
|
|
||||||
var ISLOADING = "isLoading";
|
var ISLOADING = "isLoading";
|
||||||
var THUMBNAIL_IMAGE_SOURCE = "thumbnailImageSource";
|
var THUMBNAIL_IMAGE = "thumbnailImage";
|
||||||
var IMAGE_SOURCE = "imageSource";
|
var IMAGE_SOURCE = "imageSource";
|
||||||
|
|
||||||
export class RedditViewModel extends observable.Observable {
|
export class RedditViewModel extends observable.Observable {
|
||||||
@@ -42,45 +42,39 @@ export class RedditViewModel extends observable.Observable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private _thumbnailImageSource: imageSource.ImageSource;
|
get thumbnailImage(): imageSource.ImageSource {
|
||||||
get thumbnailImageSource(): imageSource.ImageSource {
|
if (!this._source) {
|
||||||
if (this._source) {
|
return redditAppViewModel.defaultThumbnailImageSource;
|
||||||
|
}
|
||||||
|
|
||||||
if (this._source.title === "reddit 101") {
|
if (this._source.title === "reddit 101") {
|
||||||
this._thumbnailImageSource = firstThumbnailImageSource;
|
return firstThumbnailImageSource;
|
||||||
} else if (redditAppViewModel.cache) {
|
}
|
||||||
|
|
||||||
var url = this._source.thumbnail;
|
var url = this._source.thumbnail;
|
||||||
|
|
||||||
var imgSource: imageSource.ImageSource;
|
if (!_isValidImageUrl(url)) {
|
||||||
|
return redditAppViewModel.defaultNoThumbnailImageSource
|
||||||
|
}
|
||||||
|
|
||||||
var image = redditAppViewModel.cache.get(url);
|
var image = redditAppViewModel.cache.get(url);
|
||||||
if (image) {
|
if (image) {
|
||||||
imgSource = imageSource.fromNativeSource(image);
|
return image;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (imgSource) {
|
|
||||||
this._thumbnailImageSource = imgSource;
|
|
||||||
}
|
|
||||||
else if (_isValidImageUrl(url)) {
|
|
||||||
this.isLoading = true;
|
this.isLoading = true;
|
||||||
|
|
||||||
redditAppViewModel.cache.push({
|
redditAppViewModel.cache.push({
|
||||||
key: url,
|
key: url,
|
||||||
url: url,
|
url: url,
|
||||||
completed: (image: any, key: string) => {
|
completed: (image: any, key: string) => {
|
||||||
if (url === key) {
|
if (url === key) {
|
||||||
this.isLoading = false;
|
this.isLoading = false;
|
||||||
var imgSource = imageSource.fromNativeSource(image);
|
this.notify({ object: this, eventName: observable.knownEvents.propertyChange, propertyName: THUMBNAIL_IMAGE, value: image });
|
||||||
this._thumbnailImageSource = imgSource;
|
|
||||||
this.notify({ object: this, eventName: observable.knownEvents.propertyChange, propertyName: THUMBNAIL_IMAGE_SOURCE, value: imgSource });
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else {
|
|
||||||
this._thumbnailImageSource = redditAppViewModel.defaultNoThumbnailImageSource;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return this._thumbnailImageSource || redditAppViewModel.defaultThumbnailImageSource;
|
return redditAppViewModel.defaultThumbnailImageSource;
|
||||||
}
|
}
|
||||||
|
|
||||||
get imageSource(): imageSource.ImageSource {
|
get imageSource(): imageSource.ImageSource {
|
||||||
|
|||||||
@@ -1,22 +1,77 @@
|
|||||||
import common = require("ui/image-cache/image-cache-common");
|
import common = require("ui/image-cache/image-cache-common");
|
||||||
import httpRequest = require("http/http-request");
|
import httpRequest = require("http/http-request");
|
||||||
|
import utils = require("utils/utils");
|
||||||
|
import trace = require("trace");
|
||||||
|
|
||||||
module.exports.knownEvents = common.knownEvents;
|
module.exports.knownEvents = common.knownEvents;
|
||||||
|
|
||||||
|
//class NSCacheDelegateImpl extends NSObject implements NSCacheDelegate {
|
||||||
|
// public static ObjCProtocols = [NSCacheDelegate];
|
||||||
|
|
||||||
|
// static new(): NSCacheDelegateImpl {
|
||||||
|
// return <NSCacheDelegateImpl>super.new();
|
||||||
|
// }
|
||||||
|
|
||||||
|
// public cacheWillEvictObject(cache: NSCache, obj: any): void {
|
||||||
|
// trace.write("NSCacheDelegateImpl.cacheWillEvictObject(" + obj + ");", trace.categories.Debug);
|
||||||
|
// }
|
||||||
|
//}
|
||||||
|
|
||||||
|
class MemmoryWarningHandler extends NSObject {
|
||||||
|
static new(): MemmoryWarningHandler {
|
||||||
|
return <MemmoryWarningHandler>super.new();
|
||||||
|
}
|
||||||
|
|
||||||
|
private _cache: NSCache;
|
||||||
|
|
||||||
|
public initWithCache(cache: NSCache): MemmoryWarningHandler {
|
||||||
|
this._cache = cache;
|
||||||
|
|
||||||
|
NSNotificationCenter.defaultCenter().addObserverSelectorNameObject(this, "clearCache", "UIApplicationDidReceiveMemoryWarningNotification", null);
|
||||||
|
trace.write("[MemmoryWarningHandler] Added low memory observer.", trace.categories.Debug);
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public dealloc(): void {
|
||||||
|
NSNotificationCenter.defaultCenter().removeObserverNameObject(this, "UIApplicationDidReceiveMemoryWarningNotification", null);
|
||||||
|
trace.write("[MemmoryWarningHandler] Removed low memory observer.", trace.categories.Debug);
|
||||||
|
super.dealloc();
|
||||||
|
}
|
||||||
|
|
||||||
|
public clearCache(): void {
|
||||||
|
trace.write("[MemmoryWarningHandler] Clearing Image Cache.", trace.categories.Debug);
|
||||||
|
this._cache.removeAllObjects();
|
||||||
|
utils.GC();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ObjCExposedMethods = {
|
||||||
|
"clearCache": { returns: interop.types.void, params: [] }
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
export class Cache extends common.Cache {
|
export class Cache extends common.Cache {
|
||||||
private _cache: NSCache;
|
private _cache: NSCache;
|
||||||
|
//private _delegate: NSCacheDelegate;
|
||||||
|
private _memoryWarningHandler: MemmoryWarningHandler;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
super();
|
super();
|
||||||
|
|
||||||
this._cache = new NSCache();
|
this._cache = new NSCache();
|
||||||
|
|
||||||
|
//this._delegate = NSCacheDelegateImpl.new();
|
||||||
|
//this._cache.delegate = this._delegate;
|
||||||
|
|
||||||
|
this._memoryWarningHandler = MemmoryWarningHandler.new().initWithCache(this._cache);
|
||||||
}
|
}
|
||||||
|
|
||||||
public _downloadCore(request: common.DownloadRequest) {
|
public _downloadCore(request: common.DownloadRequest) {
|
||||||
var that = this;
|
var that = this;
|
||||||
httpRequest.request({ url: request.url, method: "GET" })
|
httpRequest.request({ url: request.url, method: "GET" })
|
||||||
.then(response => {
|
.then(response => {
|
||||||
var image = UIImage.imageWithData(response.content.raw);
|
var image = UIImage.alloc().initWithData(response.content.raw);
|
||||||
that._onDownloadCompleted(request.key, image);
|
that._onDownloadCompleted(request.key, image);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -35,5 +90,6 @@ export class Cache extends common.Cache {
|
|||||||
|
|
||||||
public clear() {
|
public clear() {
|
||||||
this._cache.removeAllObjects();
|
this._cache.removeAllObjects();
|
||||||
|
utils.GC();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -15,15 +15,11 @@ var IMAGE = "Image";
|
|||||||
var ISLOADING = "isLoading";
|
var ISLOADING = "isLoading";
|
||||||
var STRETCH = "stretch";
|
var STRETCH = "stretch";
|
||||||
|
|
||||||
function isValidSrc(src: any): boolean {
|
|
||||||
return types.isString(src);
|
|
||||||
}
|
|
||||||
|
|
||||||
function onSrcPropertyChanged(data: dependencyObservable.PropertyChangeData) {
|
function onSrcPropertyChanged(data: dependencyObservable.PropertyChangeData) {
|
||||||
var image = <Image>data.object;
|
var image = <Image>data.object;
|
||||||
var value = data.newValue;
|
var value = data.newValue;
|
||||||
|
|
||||||
if (isValidSrc(value)) {
|
if (types.isString(value)) {
|
||||||
value = value.trim();
|
value = value.trim();
|
||||||
image.imageSource = null;
|
image.imageSource = null;
|
||||||
image["_url"] = value;
|
image["_url"] = value;
|
||||||
@@ -46,6 +42,9 @@ function onSrcPropertyChanged(data: dependencyObservable.PropertyChangeData) {
|
|||||||
// Support binding the iamgeSource trough the src propoerty
|
// Support binding the iamgeSource trough the src propoerty
|
||||||
image.imageSource = value;
|
image.imageSource = value;
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
image._setNativeImage(value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Image extends view.View implements definition.Image {
|
export class Image extends view.View implements definition.Image {
|
||||||
@@ -54,7 +53,7 @@ export class Image extends view.View implements definition.Image {
|
|||||||
SRC,
|
SRC,
|
||||||
IMAGE,
|
IMAGE,
|
||||||
new proxy.PropertyMetadata(
|
new proxy.PropertyMetadata(
|
||||||
"",
|
undefined,
|
||||||
dependencyObservable.PropertyMetadataSettings.None,
|
dependencyObservable.PropertyMetadataSettings.None,
|
||||||
onSrcPropertyChanged
|
onSrcPropertyChanged
|
||||||
)
|
)
|
||||||
@@ -99,10 +98,10 @@ export class Image extends view.View implements definition.Image {
|
|||||||
this._setValue(Image.imageSourceProperty, value);
|
this._setValue(Image.imageSourceProperty, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
get src(): string {
|
get src(): any {
|
||||||
return this._getValue(Image.srcProperty);
|
return this._getValue(Image.srcProperty);
|
||||||
}
|
}
|
||||||
set src(value: string) {
|
set src(value: any) {
|
||||||
this._setValue(Image.srcProperty, value);
|
this._setValue(Image.srcProperty, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -117,6 +116,10 @@ export class Image extends view.View implements definition.Image {
|
|||||||
this._setValue(Image.stretchProperty, value);
|
this._setValue(Image.stretchProperty, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public _setNativeImage(nativeImage: any) {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
public onMeasure(widthMeasureSpec: number, heightMeasureSpec: number): void {
|
public onMeasure(widthMeasureSpec: number, heightMeasureSpec: number): void {
|
||||||
|
|
||||||
// We don't call super because we measure native view with specific size.
|
// We don't call super because we measure native view with specific size.
|
||||||
|
|||||||
@@ -36,9 +36,7 @@ function onImageSourcePropertyChanged(data: dependencyObservable.PropertyChangeD
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (image.android) {
|
image._setNativeImage(data.newValue ? data.newValue.android : null);
|
||||||
image.android.setImageBitmap(data.newValue ? data.newValue.android : null);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// register the setNativeValue callback
|
// register the setNativeValue callback
|
||||||
@@ -55,4 +53,8 @@ export class Image extends imageCommon.Image {
|
|||||||
public _createUI() {
|
public _createUI() {
|
||||||
this._android = new android.widget.ImageView(this._context);
|
this._android = new android.widget.ImageView(this._context);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public _setNativeImage(nativeImage: any) {
|
||||||
|
this.android.setImageBitmap(nativeImage);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
4
ui/image/image.d.ts
vendored
4
ui/image/image.d.ts
vendored
@@ -31,9 +31,9 @@ declare module "ui/image" {
|
|||||||
imageSource: imageSource.ImageSource;
|
imageSource: imageSource.ImageSource;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets or sets the URL of the image.
|
* Gets or sets the source of the Image. This can be either an URL string or a native image instance.
|
||||||
*/
|
*/
|
||||||
src: string;
|
src: any;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets a value indicating if the image is currently loading
|
* Gets a value indicating if the image is currently loading
|
||||||
|
|||||||
@@ -30,17 +30,15 @@ function onStretchPropertyChanged(data: dependencyObservable.PropertyChangeData)
|
|||||||
|
|
||||||
function onImageSourcePropertyChanged(data: dependencyObservable.PropertyChangeData) {
|
function onImageSourcePropertyChanged(data: dependencyObservable.PropertyChangeData) {
|
||||||
var image = <Image>data.object;
|
var image = <Image>data.object;
|
||||||
image.ios.image = data.newValue ? data.newValue.ios : null;
|
image._setNativeImage(data.newValue ? data.newValue.ios : null);
|
||||||
|
|
||||||
if (isNaN(image.width) || isNaN(image.height)) {
|
|
||||||
image.requestLayout();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// register the setNativeValue callback
|
// register the setNativeValue callback
|
||||||
(<proxy.PropertyMetadata>imageCommon.Image.imageSourceProperty.metadata).onSetNativeValue = onImageSourcePropertyChanged;
|
(<proxy.PropertyMetadata>imageCommon.Image.imageSourceProperty.metadata).onSetNativeValue = onImageSourcePropertyChanged;
|
||||||
(<proxy.PropertyMetadata>imageCommon.Image.stretchProperty.metadata).onSetNativeValue = onStretchPropertyChanged;
|
(<proxy.PropertyMetadata>imageCommon.Image.stretchProperty.metadata).onSetNativeValue = onStretchPropertyChanged;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
export class Image extends imageCommon.Image {
|
export class Image extends imageCommon.Image {
|
||||||
private _ios: UIImageView;
|
private _ios: UIImageView;
|
||||||
|
|
||||||
@@ -57,4 +55,12 @@ export class Image extends imageCommon.Image {
|
|||||||
get ios(): UIImageView {
|
get ios(): UIImageView {
|
||||||
return this._ios;
|
return this._ios;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public _setNativeImage(nativeImage: any) {
|
||||||
|
this.ios.image = nativeImage;
|
||||||
|
|
||||||
|
if (isNaN(this.width) || isNaN(this.height)) {
|
||||||
|
this.requestLayout();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user