Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios7 是否使用不带ImageName的xcassets来防止内存问题?_Ios7_Memory Leaks_Imagenamed_Xcasset - Fatal编程技术网

Ios7 是否使用不带ImageName的xcassets来防止内存问题?

Ios7 是否使用不带ImageName的xcassets来防止内存问题?,ios7,memory-leaks,imagenamed,xcasset,Ios7,Memory Leaks,Imagenamed,Xcasset,根据apple文档,建议将xcassets用于iOS7应用程序,并通过ImageName引用这些图像 但据我所知,ImageName和内存总是有问题 因此,我做了一个简短的测试应用程序-引用xcassets目录中的图像,并使用imageNamed启动探查器。。。结果如预期。一旦分配内存,即使我从superview中删除ImageView并将其设置为nil,也不会再次释放 我目前正在开发一款iPad应用程序,其中有许多大图像,这种奇怪的imageView行为会导致内存警告 但在我的测试中,我无法通

根据apple文档,建议将xcassets用于iOS7应用程序,并通过ImageName引用这些图像

但据我所知,ImageName和内存总是有问题

因此,我做了一个简短的测试应用程序-引用xcassets目录中的图像,并使用imageNamed启动探查器。。。结果如预期。一旦分配内存,即使我从superview中删除ImageView并将其设置为nil,也不会再次释放

我目前正在开发一款iPad应用程序,其中有许多大图像,这种奇怪的imageView行为会导致内存警告

但在我的测试中,我无法通过带有文件内容的图像访问xcassets图像

那么,在iOS7上处理大型图像的最佳方法是什么?是否有其他(更高性能)方式访问xcassets目录中的图像?或者我根本不应该使用xcassets,这样我就可以使用带有文件内容的图像了


谢谢你的回答

更新:缓存逐出会产生罚款(至少从iOS 8.3开始)

我也决定使用苹果公司的“new Images.xcassets”。事情开始变得糟糕,当时我的应用程序中有大约350mb的图像,应用程序不断崩溃(在视网膜iPad上;可能是因为加载图像的大小)

我已经编写了一个非常简单的测试应用程序,我在其中加载了三种不同类型的图像(查看分析器):

  • imagename:
    从资产加载:图像永远不会发布,应用程序崩溃(对我来说,我可以加载400个图像,但这取决于图像大小)

  • ImageName:
    (通常包括在项目中):内存使用率很高,偶尔(>400张图像)我会看到调用
    didReceiveMemoryWarning:
    ,但应用程序运行正常


  • imageWithContentsOfFile([[NSBundle mainBundle]path for resource:…)
    :内存使用率非常低(我想知道你是否能够分享你最终是如何做到这一点的?非常感谢。xcassets没有。我使用的是旧方法-将图像放在我的项目文件夹中,然后通过imageWithContentsOfFile访问它们。
    缓存逐出工程
    的含义是什么?这意味着我们可以在我的iOS 8.3+中直接使用ImageName:?@Noobie。)意见,ImageName:从来都不是问题。仅适用于Images.xAssets;但由于8.3+版本,它也很好。你的意思是它不再缓存图像?它确实缓存图像,但也会在内存不足的情况下从缓存中逐出它们。然而,问题不在于缓存逐出是否有效,而在于首先如何防止缓存。如果你知道这一点你只需要使用一次图像就可以将其粘贴到你的界面中,而且缓存它完全是浪费内存。苹果公司明确表示在这种情况下使用
    init(contentsOfFile:)
    ,但它不能用于资产目录。这仍然是一个未解决的问题。
    cd projectFolderWithImageAsset
    echo "{\"assets\": [" > a.json
    find Images.xcassets/ -name \*.json | while read jsonfile; do
      tmppath=${jsonfile%.imageset/*}
      assetname=${tmppath##*/}
      echo "{\"assetname\":\"${assetname}\",\"content\":" >> a.json
      cat $jsonfile >> a.json; 
      echo '},' >>a.json
    done
    echo ']}' >>a.json
    
    #import "UIImage+Extension.h"
    #import <objc/objc-runtime.h>
    #import "IMGADataModels.h"
    
    @implementation UIImage (UIImage_Extension)
    
    
    + (void)load{
    static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            Class class = [self class];
            Method imageNamed = class_getClassMethod(class, @selector(imageNamed:));
            Method imageNamedCustom = class_getClassMethod(class, @selector(imageNamedCustom:));
            method_exchangeImplementations(imageNamed, imageNamedCustom);
        });
    }
    
    + (IMGABaseClass*)model {
        static NSString * const jsonFile = @"a";
        static IMGABaseClass *baseClass = nil;
    
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            NSString *fileFilePath = [[NSBundle mainBundle] pathForResource:jsonFile ofType:@"json"];
            NSData* myData = [NSData dataWithContentsOfFile:fileFilePath];
            __autoreleasing NSError* error = nil;
            id result = [NSJSONSerialization JSONObjectWithData:myData
                                                        options:kNilOptions error:&error];
            if (error != nil) {
                ErrorLog(@"Could not load file %@. The App will be totally broken!!!", jsonFile);
            } else {
                baseClass = [[IMGABaseClass alloc] initWithDictionary:result];
            }
        });
        return baseClass;
    }
    
    
    + (UIImage *)imageNamedCustom:(NSString *)name{
    
        NSString *imageFileName = nil;
        IMGAContent *imgContent = nil;
        CGFloat scale = 2;
    
        for (IMGAAssets *asset in [[self model] assets]) {
            if ([name isEqualToString: [asset assetname]]) {
                imgContent = [asset content];
                break;
            }
        }
        if (!imgContent) {
            ErrorLog(@"No image named %@ found", name);
        }
    
        if (is4InchScreen) {
            for (IMGAImages *image in [imgContent images]) {
                if ([@"retina4" isEqualToString:[image subtype]]) {
                    imageFileName = [image filename];
                    break;
                }
            }
        } else {
            if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone ) {
                for (IMGAImages *image in [imgContent images]) {
                    if ([@"iphone" isEqualToString:[image idiom]] && ![@"retina4" isEqualToString:[image subtype]]) {
                        imageFileName = [image filename];
                        break;
                    }
                }
            } else {
                if (isRetinaScreen) {
                    for (IMGAImages *image in [imgContent images]) {
                        if ([@"universal" isEqualToString:[image idiom]] && [@"2x" isEqualToString:[image scale]]) {
                            imageFileName = [image filename];
                            break;
                        }
                    }
                } else {
                    for (IMGAImages *image in [imgContent images]) {
                        if ([@"universal" isEqualToString:[image idiom]] && [@"1x" isEqualToString:[image scale]]) {
                            imageFileName = [image filename];
                            if (nil == imageFileName) {
                                // fallback to 2x version for iPad unretina
                                for (IMGAImages *image in [imgContent images]) {
                                    if ([@"universal" isEqualToString:[image idiom]] && [@"2x" isEqualToString:[image scale]]) {
                                        imageFileName = [image filename];
                                        break;
                                    }
                                }
                            } else {
                                scale = 1;
                                break;
                            }
                        }
                    }
                }
            }
        }
    
        if (!imageFileName) {
            ErrorLog(@"No image file name found for named image %@", name);
        }
    
        NSString *imageName = [[NSBundle mainBundle] pathForResource:imageFileName ofType:@""];
        NSData *imgData = [NSData dataWithContentsOfFile:imageName];
        if (!imgData) {
            ErrorLog(@"No image file found for named image %@", name);
        }
        UIImage *image = [UIImage imageWithData:imgData scale:scale];
        DebugVerboseLog(@"%@", imageFileName);
        return image;
    }
    
    @end