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];
     */
                이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
IOS에서 ReplayKit 및 RTC 사용 방법응용된 소리와 아나운서의 소리를 포함한다.이 두 가지 수요를 감안하여 우리는 스크린 공유를 하는 생방송에 필요한 미디어 흐름을 간단하게 분석할 수 있다. 만약 우리가 Audio App과 Audio Mic를 두 개의 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.