iOS 자동 으로 그림 생 성 @ 1x, @ 2x, @ 3x 그림

11161 단어 IOS
다운로드 주소:
https://github.com/odot/AutoGenerateSuitImage
핵심 클래스
//
//  UIImage+AutoResize.h
//  CommonToolLib
//
//  Created by [email protected] on 15/2/2.
//  Copyright (c) 2015  todot. All rights reserved.
//

#import 

/*
 *           
 * DOTOriginImageType3x [email protected]       
 * DOTOriginImageType2x [email protected]    
 * DOTOriginImageType1x imageName.png    
 */
typedef NS_ENUM(NSInteger, DOTOriginImageType) {
    DOTOriginImageType3x    = 0,
    DOTOriginImageType2x    = 1,
    DOTOriginImageType1x    = 2
};

@interface UIImage(AutoResize)

+ (void)generateSuitImagesWithOriginImagePath:(NSString*)originPath
                                 newImagePath:(NSString*)newPath;

+ (void)generateSuitImagesWithOriginImagePath:(NSString*)originPath
                                 newImagePath:(NSString*)newPath
                              originImageType:(DOTOriginImageType)originImageType;

/*
 *                 (         @3x  ,         @2x        )
 *
 * @param originPath               
 * @param newPath                   ,   nil, nil             originPath/newImages  
 * @param originImageType         (@3x、@2x、@1x,  @1x       )
 * @param autoEnLarge             (          @3x ,enLarge=YES     @3x     )
 *
 * @return void
 */

+ (void)generateSuitImagesWithOriginImagePath:(NSString*)originPath
                                 newImagePath:(NSString*)newPath
                              originImageType:(DOTOriginImageType)originImageType
                     autoGenerateEnLargeImage:(BOOL)enLarge;
@end

파일
//
//  UIImage+AutoResize.m
//  CommonToolLib
//
//  Created by [email protected] on 15/2/2.
//  Copyright (c) 2015  todot. All rights reserved.
//

#import "UIImage+AutoResize.h"

@implementation UIImage(AutoResize)

//     
+ (BOOL)createDirectory:(NSString*)directory
{
    NSError* error = nil;
    BOOL isCreatDirSucc = [[NSFileManager defaultManager] createDirectoryAtPath:directory withIntermediateDirectories:YES attributes:nil error:&error];
    if (!isCreatDirSucc && !error) {
        NSLog(@"  %@    :%@", directory, error);
        return NO;
    } else {
        NSLog(@"  %@    ", directory);
        return YES;
    }
}

//         
+ (BOOL)isDirectoryExist:(NSString*)directory
{
    BOOL isDir = NO;
    
    //             
    BOOL isDirExist = [[NSFileManager defaultManager] fileExistsAtPath:directory isDirectory:&isDir];
    if (!isDirExist && !isDir) {
        if ([self createDirectory:directory] == NO) {
            return NO;
        }
    }
    isDirExist = [[NSFileManager defaultManager] fileExistsAtPath:directory];
    
    return isDirExist;
}

+ (UIImage*)originImage:(UIImage *)image scaleToSize:(CGSize)size
{
    //     bitmap context
    //               context
    UIGraphicsBeginImageContext(size);
    
    //          
    [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
    
    //    context             
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    
    //     context   
    UIGraphicsEndImageContext();
    
    //             
    return scaledImage;
}

//       
+ (UIImage*)originImage:(UIImage *)image scaleWithMultiple:(CGFloat)multiple
{
    CGSize size = image.size;
    //     bitmap context
    //               context
    UIGraphicsBeginImageContext(size);
    
    //          
    [image drawInRect:CGRectMake(0, 0, size.width * multiple, size.height * multiple)];
    
    //    context             
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    
    //     context   
    UIGraphicsEndImageContext();
    
    //             
    return scaledImage;
}

+ (void)generateSuitImagesWithOriginImagePath:(NSString*)originPath
                                 newImagePath:(NSString*)newPath
{
    [self generateSuitImagesWithOriginImagePath:originPath newImagePath:newPath originImageType:DOTOriginImageType3x autoGenerateEnLargeImage:NO];
}

+ (void)generateSuitImagesWithOriginImagePath:(NSString*)originPath
                                 newImagePath:(NSString*)newPath
                              originImageType:(DOTOriginImageType)originImageType
{
    [self generateSuitImagesWithOriginImagePath:originPath newImagePath:newPath originImageType:originImageType autoGenerateEnLargeImage:NO];
}

/*
 *                 (         @3x  ,         @2x        )
 *
 * @param originPath               
 * @param newPath                   ,   nil, nil             originPath/newImages  
 * @param originImageType         (@3x、@2x、@1x,  @1x       )
 * @param autoEnLarge             (          @3x ,enLarge=YES     @3x     )
 *
 * @return void
 */

+ (void)generateSuitImagesWithOriginImagePath:(NSString*)originPath
                                 newImagePath:(NSString*)newPath
                              originImageType:(DOTOriginImageType)originImageType
                     autoGenerateEnLargeImage:(BOOL)enLarge
{
    NSFileManager* fileManager = [NSFileManager defaultManager];
    NSLog(@"------   %@    ------",fileManager.currentDirectoryPath);
    BOOL isDir = NO;
    BOOL isDirExist = [fileManager fileExistsAtPath:originPath isDirectory:&isDir];
    if (isDir == NO || isDirExist == NO) {
        NSLog(@"%@ %@   ", NSStringFromClass([self class]), originPath);
        return;
    }
    
    NSString* newImagePath = [NSString stringWithFormat:@"%@", newPath];
    
    if (newPath == nil || newPath.length == 0) {
        newImagePath = [originPath stringByAppendingPathComponent:@"newImages"];
    }
    
    //                
    if ([self isDirectoryExist:newImagePath] == NO) {
        NSLog(@"%@ %@  ", NSStringFromClass([self class]), newImagePath);
        return;
    }
    
    NSString* tmpImagePath = [newImagePath stringByAppendingPathComponent:@"tmp"];
    if ([self isDirectoryExist:tmpImagePath] == NO) {
        NSLog(@"%@       [%@]  ", NSStringFromClass([self class]), tmpImagePath);
        return;
    }
   
    //         
    NSDirectoryEnumerator* direnum = [fileManager enumeratorAtPath:originPath];
    NSMutableArray *files = [NSMutableArray array];
    NSString *filename ;
    while (filename = [direnum nextObject]) {
        if ([[filename pathExtension] isEqualToString:@"png"]) {
            [files addObject: filename];
        }
    }
    
    NSLog(@"%@ %@", NSStringFromClass([self class]), files);
    
    for (NSString* filename in files) {
        NSString* prefix = [[filename componentsSeparatedByString:@"."] objectAtIndex:0];
        
        
        NSString* filePrefix = [NSString stringWithString:prefix];
        NSRange range = [prefix rangeOfString:@"@"];
        if (range.length != 0) {
            //    @      @3x, @2x   
            filePrefix = [[prefix componentsSeparatedByString:@"@"] objectAtIndex:0];
        }
        
        
        //     
        NSString* oldPath = [originPath stringByAppendingPathComponent:filename];
        NSString* tmpNewImagePath = [[tmpImagePath stringByAppendingPathComponent:filePrefix] stringByAppendingPathExtension:@"png"];
        
        NSString* bigPath = [[newImagePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@@3x", filePrefix]] stringByAppendingPathExtension:@"png"];
        NSString* middlePath = [[newImagePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@@2x", filePrefix]] stringByAppendingPathExtension:@"png"];
        NSString* smallPath = [[newImagePath stringByAppendingPathComponent:filePrefix] stringByAppendingPathExtension:@"png"];

        /*
                    @3x/@2x            @3x/@2x   (      ,            ),     
         
            UIImage    
         */
        NSError* error = nil;
        
        BOOL copySucc = [[NSFileManager defaultManager] copyItemAtPath:oldPath toPath:tmpNewImagePath error:&error];
        if (copySucc && error == nil) {
            if (originImageType == DOTOriginImageType3x) {
                [self saveResizedImageWithOriginPath:tmpNewImagePath toNewPath:bigPath WithMultiple:1.0];
                [self saveResizedImageWithOriginPath:tmpNewImagePath toNewPath:middlePath WithMultiple:0.667];
                [self saveResizedImageWithOriginPath:tmpNewImagePath toNewPath:smallPath WithMultiple:0.334];
                
            } else if (originImageType ==DOTOriginImageType2x) {
                [self saveResizedImageWithOriginPath:tmpNewImagePath toNewPath:middlePath WithMultiple:1.0];
                [self saveResizedImageWithOriginPath:tmpNewImagePath toNewPath:smallPath WithMultiple:0.5];
                if (enLarge == YES) {
                    [self saveResizedImageWithOriginPath:tmpNewImagePath toNewPath:bigPath WithMultiple:1.5];
                }
                
            } else if (originImageType == DOTOriginImageType1x) {
                [self saveResizedImageWithOriginPath:tmpNewImagePath toNewPath:smallPath WithMultiple:1.0];
                
                if (enLarge == YES) {
                    [self saveResizedImageWithOriginPath:tmpNewImagePath toNewPath:middlePath WithMultiple:2.0];
                    [self saveResizedImageWithOriginPath:tmpNewImagePath toNewPath:bigPath WithMultiple:3.0];
                }
            }
        }
    }
}

+ (BOOL)saveResizedImageWithOriginPath:(NSString*)originPath toNewPath:(NSString*)newPath WithMultiple:(CGFloat)multiple
{
    UIImage* originImage = [UIImage imageNamed:originPath];
    
    CGSize newSize = CGSizeMake(originImage.size.width * multiple, originImage.size.height * multiple);
    UIImage* newImage = [self originImage:originImage scaleToSize:newSize];
    
    BOOL scaleSucc = [UIImagePNGRepresentation(newImage) writeToFile:newPath atomically:YES];
    if (scaleSucc == YES) {
        NSLog(@"%lf:%@---->%@  ", multiple, originPath, newPath);
    }
    return scaleSucc;
}

@end

호출 방법 은 다음 과 같다. 
/Users/hebiao/Desktop/cut3x                 ,                
  [UIImage generateSuitImagesWithOriginImagePath:@"/Users/hebiao/Desktop/cut3x" newImagePath:nil originImageType:DOTOriginImageType3x autoGenerateEnLargeImage:NO];
    
   
    //         @2x    autoGenerateEnLargeImage=YES          @3x  
    [UIImage generateSuitImagesWithOriginImagePath:@"/Users/hebiao/Desktop/cut2x" newImagePath:nil originImageType:DOTOriginImageType2x autoGenerateEnLargeImage:YES];
     /*
    //                 autoGenerateEnLargeImage=YES          @3x @2x  (      )
    [UIImage generateSuitImagesWithOriginImagePath:@"/Users/hebiao/Desktop/cut2x" newImagePath:nil originImageType:DOTOriginImageType1x autoGenerateEnLargeImage:YES];
     */

좋은 웹페이지 즐겨찾기