逆向iOS SDK -- _UIImageAtPath 的实现(SDK 5.1)

注释过的反汇编代码:http://pan.baidu.com/share/link?shareid=3491166579&uk=537224442

伪代码(不精确,仅供参考): 

 

NSString* _UICacheNameForImageAtPath(NSString *imageName,NSBundle *bundle);

NSString* ProductSuffix();

UIImage* GetImageAtPath(NSString *imageFilePath,CGFloat scale);

 

NSMutableDictionary *gCacheNameToImageMap =nil;

NSMutableDictionary *gImageToCacheNameMap =nil;

BOOL __prefer2xImages = NO;

 

UIImage *_UIImageAtPath(NSString *imageFileName,NSBundle *mainBundle, BOOL shouldForce1xScale)

{

    // imageFileName = @"Default.png"

    if ([imageFileName length] == 0)

        return nil;

   

    // bundleIdentifier_imageFileName

    NSString *cacheNameOfImage =_UICacheNameForImageAtPath(imageFileName, mainBundle);

    UIImage *resultImage = nil;

    if (gCacheNameToImageMap !=nil)

    {

        resultImage = [gCacheNameToImageMapobjectForKey:cacheNameOfImage];

        if (resultImage != nil)

        {

            if (![resultImage _isCached])

            {

                [resultImage retain];

            }

           

            [resultImage _setCached:YES];

           

            return resultImage;

        }

    }

    else

    {

        gCacheNameToImageMap = [NSMutableDictionarydictionary];

        gImageToCacheNameMap = [NSMutableDictionarydictionary];

    }

   

    BOOL force1xScale = NO;

    if (__prefer2xImages)

    {

        force1xScale = shouldForce1xScale;

    }

   

    NSString *imageExt = [imageFileNamepathExtension];

    if ([imageExt length] == 0)

    {

        imageExt = @"png";

    }

   

    NSString *bundlePath = nil;

    if (mainBundle != nil)

    {

        bundlePath = [mainBundle bundlePath];

    }

   

    NSString *productSuffix = ProductSuffix();// ~iphone, ~ipad

    NSString *imageNameWithoutSuffix = [imageFileNamestringByReplacingOccurrencesOfString:productSuffixwithString:@""];                   

   

    // Default

    NSString *imageNameWithoutSuffixAndExt = [imageNameWithoutSuffixstringByDeletingPathExtension];

    // Default~iphone

    NSString *imageNameWithSuffix = [imageNameWithoutSuffixAndExtstringByAppendingString:productSuffix];

    // Default@1x

    NSString *imageName1x = [imageNameWithoutSuffixAndExtstringByAppendingString:@"@1x"];

    // Default@1x~iphone

    NSString *imageName1xWithSuffix = [imageName1xstringByAppendingString:productSuffix];

    // Default_1only_

    NSString *imageName_1only_ = [imageNameWithoutSuffixAndExtstringByAppendingString:@"_1only_"];

    // Default_1only_~ipnone

    NSString *imageName_1only_WithSuffix = [imageName_1only_stringByAppendingString:productSuffix];

   

    // Default@2x

    NSString *imageName2x = [imageNameWithoutSuffixAndExtstringByAppendingString:@"@2x"];

    // Default@2x~iphone

    NSString *imageName2xWithSuffix = [imageName2xstringByAppendingString:productSuffix];

    // Default_2only_@2x

    NSString *imageName_2only_2x = [imageNameWithoutSuffixAndExtstringByAppendingString:@"_2only_@2x"];

    // Default_2only_@2x~iphone

    NSString *imageName_2only_2xWithSuffix = [imageName_2only_2xstringByAppendingString:productSuffix];

   

    NSString *targetFileName =nil;

    NSString *targetFilePath =nil;

    if (!force1xScale)

    {

        // Default@2x~iphone.png

        targetFileName = [imageName2xWithSuffix stringByAppendingPathExtension:imageExt];

        targetFilePath = [bundlePath stringByAppendingPathComponent:targetFileName];

        resultImage = GetImageAtPath(targetFilePath,2.0f);

       

        // Default_2only_@2x~iphone.png

        if (resultImage == nil)

        {

            targetFileName = [imageName_2only_2xWithSuffix stringByAppendingPathExtension:imageExt];

            targetFilePath = [bundlePath stringByAppendingPathComponent:targetFileName];

            resultImage = GetImageAtPath(targetFilePath,2.0f);

        }

       

        // Default@2x.png

        if (resultImage == nil)

        {

            targetFileName = [imageName2x stringByAppendingPathExtension:imageExt];

            targetFilePath = [bundlePath stringByAppendingPathComponent:targetFileName];

            resultImage = GetImageAtPath(targetFilePath,2.0f);

        }

       

        // Default_2only_@2x.png

        if (resultImage == nil)

        {

            targetFileName = [imageName_2only_2x stringByAppendingPathExtension:imageExt];

            targetFilePath = [bundlePath stringByAppendingPathComponent:targetFileName];

            resultImage = GetImageAtPath(targetFilePath,2.0f);

        }

    }

   

    if (resultImage == nil)

    {

        // Default~iphone.png

        targetFileName = [imageNameWithSuffix stringByAppendingPathExtension:imageExt];

        targetFilePath = [bundlePath stringByAppendingPathComponent:targetFileName];

        resultImage = GetImageAtPath(targetFilePath,1.0f);

       

        // Default.png

        if (resultImage == nil)

        {

            targetFileName = [imageNameWithoutSuffixAndExt stringByAppendingPathExtension:imageExt];

            targetFilePath = [bundlePath stringByAppendingPathComponent:targetFileName];

            resultImage = GetImageAtPath(targetFilePath,1.0f);

        }

       

        // Default@1x~iphone.png

        if (resultImage == nil)

        {

            targetFileName = [imageName1xWithSuffix stringByAppendingPathExtension:imageExt];

            targetFilePath = [bundlePath stringByAppendingPathComponent:targetFileName];

            resultImage = GetImageAtPath(targetFilePath,1.0f);

        }

       

        // Default_1only_~ipnone.png

        if (resultImage == nil)

        {

            targetFileName = [imageName_1only_WithSuffix stringByAppendingPathExtension:imageExt];

            targetFilePath = [bundlePath stringByAppendingPathComponent:targetFileName];

            resultImage = GetImageAtPath(targetFilePath,1.0f);

        }

       

        // Default@1x.png

        if (resultImage == nil)

        {

            targetFileName = [imageName1x stringByAppendingPathExtension:imageExt];

            targetFilePath = [bundlePath stringByAppendingPathComponent:targetFileName];

            resultImage = GetImageAtPath(targetFilePath,1.0f);

        }

       

        // Default_1only.png

        if (resultImage == nil)

        {

            targetFileName = [imageName_1only_ stringByAppendingPathExtension:imageExt];

            targetFilePath = [bundlePath stringByAppendingPathComponent:targetFileName];

            resultImage = GetImageAtPath(targetFilePath,1.0f);

        }

       

        // Default~iphone

        if (resultImage == nil)

        {

            targetFilePath = [bundlePath stringByAppendingPathComponent:imageNameWithSuffix];

            resultImage = GetImageAtPath(targetFilePath,1.0f);

        }

       

        // Default

        if (resultImage == nil)

        {

            targetFilePath = [bundlePath stringByAppendingPathComponent:imageNameWithoutSuffixAndExt];

            resultImage = GetImageAtPath(targetFilePath,1.0f);

        }

    }

   

    if (resultImage == nil)

    {

        if (!force1xScale)

        {

            // Default@2x.png

            targetFilePath = [mainBundle pathForResource:imageName2x ofType:imageExt];

            resultImage = GetImageAtPath(targetFilePath,2.0f);

           

            // Default_2only_@2x.png

            if (resultImage ==nil)

            {

                targetFilePath = [mainBundle pathForResource:imageName_2only_2x ofType:imageExt];

                resultImage = GetImageAtPath(targetFilePath,2.0f);

            }

        }

       

        // Default.png

        if (resultImage == nil)

        {

            targetFilePath = [mainBundle pathForResource:imageNameWithoutSuffixAndExt ofType:imageExt];

            resultImage = GetImageAtPath(targetFilePath,1.0f);

        }

       

        // Default@1x.png

        if (resultImage == nil)

        {

            targetFilePath = [mainBundle pathForResource:imageName1x ofType:imageExt];

            resultImage = GetImageAtPath(targetFilePath,1.0f);

        }

       

        // Default_1only_.png

        if (resultImage == nil)

        {

            targetFilePath = [mainBundle pathForResource:imageName_1only_ ofType:imageExt];

            resultImage = GetImageAtPath(targetFilePath,1.0f);

        }

    }

   

    if (resultImage != nil)

    {

        [gCacheNameToImageMap setValue:resultImage forKey:cacheNameOfImage];

        [gImageToCacheNameMap setValue:cacheNameOfImage forKey:resultImage];

        [resultImage _setNamed:YES];

        [resultImage _setCached:YES];

    }

   

    return resultImage;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值