IOS 方法类

来源:互联网 发布:鼠标宏设置软件 编辑:程序博客网 时间:2024/05/17 01:19
1 将原图进行剪切成固定 n*m大小的图片
-(UIImage *)sizeIMg:(CGSize )size{    UIGraphicsBeginImageContext(size);        // Tell the old image to draw in this new context, with the desired    // new size    [self drawInRect:CGRectMake(0,0,size.width,size.height)];        // Get the new image from the context    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();        // End the context    UIGraphicsEndImageContext();        // Return the new image.    return newImage;}

2 调节原来图片的灰度,1.0就是黑白的

- (UIImage*)grayscale:(int)type {        CGImageRef imageRef = self.CGImage;        size_t width  = CGImageGetWidth(imageRef);    size_t height = CGImageGetHeight(imageRef);        size_t bitsPerComponent = CGImageGetBitsPerComponent(imageRef);    size_t bitsPerPixel = CGImageGetBitsPerPixel(imageRef);        size_t bytesPerRow = CGImageGetBytesPerRow(imageRef);        CGColorSpaceRef colorSpace = CGImageGetColorSpace(imageRef);        CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef);            bool shouldInterpolate = CGImageGetShouldInterpolate(imageRef);        CGColorRenderingIntent intent = CGImageGetRenderingIntent(imageRef);        CGDataProviderRef dataProvider = CGImageGetDataProvider(imageRef);        CFDataRef data = CGDataProviderCopyData(dataProvider);        UInt8 *buffer = (UInt8*)CFDataGetBytePtr(data);        NSUInteger  x, y;    for (y = 0; y < height; y++) {        for (x = 0; x < width; x++) {            UInt8 *tmp;            tmp = buffer + y * bytesPerRow + x * 4;                        UInt8 red,green,blue;            red = *(tmp + 0);            green = *(tmp + 1);            blue = *(tmp + 2);                        UInt8 brightness;            switch (type) {                case 1:                    brightness = (77 * red + 28 * green + 151 * blue) / 256;                    *(tmp + 0) = brightness;                    *(tmp + 1) = brightness;                    *(tmp + 2) = brightness;                    break;                case 2:                    *(tmp + 0) = red;                    *(tmp + 1) = green * 0.7;                    *(tmp + 2) = blue * 0.4;                    break;                case 3:                    *(tmp + 0) = 255 - red;                    *(tmp + 1) = 255 - green;                    *(tmp + 2) = 255 - blue;                    break;                default:                    *(tmp + 0) = red;                    *(tmp + 1) = green;                    *(tmp + 2) = blue;                    break;            }        }    }            CFDataRef effectedData = CFDataCreate(NULL, buffer, CFDataGetLength(data));        CGDataProviderRef effectedDataProvider = CGDataProviderCreateWithCFData(effectedData);        CGImageRef effectedCgImage = CGImageCreate(                                               width, height,                                               bitsPerComponent, bitsPerPixel, bytesPerRow,                                               colorSpace, bitmapInfo, effectedDataProvider,                                               NULL, shouldInterpolate, intent);        UIImage *effectedImage = [[UIImage alloc] initWithCGImage:effectedCgImage];        CGImageRelease(effectedCgImage);        CFRelease(effectedDataProvider);        CFRelease(effectedData);        CFRelease(data);        return effectedImage ;    }

3 base64编码

-(NSString *)base64Encode{    NSData *data = [self dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];    NSData *encodeData = [GTMBase64 encodeData:data];    return [[NSString alloc] initWithData:encodeData encoding:NSUTF8StringEncoding];}

4 获取沙盒文档目录

-(NSString *)getDocumentPath{        //获取沙盒中的文档目录    NSArray *documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);            NSString *documentDirectory=[documentDirectories objectAtIndex:0];        return [documentDirectory stringByAppendingPathComponent:self];}

5 获取沙盒temp目录

-(NSString *)getTempPath{    //获取沙盒中的文档目录//    NSArray *documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);            NSString *documentDirectory=NSTemporaryDirectory(); ;        return [documentDirectory stringByAppendingPathComponent:self];}

6 查找文件是否存在

-(BOOL)fileIsHaved{    NSFileManager *fileMgr = [NSFileManager defaultManager];    BOOL ishave = [fileMgr fileExistsAtPath:self];    if (!ishave) {        return NO;    }    NSData *data = [NSData dataWithContentsOfFile:self];    if (!data || [data length] <= 0) {        return NO;    }        return YES;}

7 查找文件夹是否存在

-(BOOL)folderIsHaved{    NSFileManager *fileMgr = [NSFileManager defaultManager];    return  [fileMgr fileExistsAtPath:self];}

8 移动文件到指定位置

-(void)fileMoveTo:(NSString *)path{     NSFileManager *fileMgr = [NSFileManager defaultManager];    [fileMgr moveItemAtPath:self toPath:path error:nil];}

9 删除文件

-(void)delFile{    NSFileManager *fileMgr = [NSFileManager defaultManager];    [fileMgr removeItemAtPath:self error:nil];}

10 文件大小

-(long long)fileSize{    struct stat st;    if (lstat([self cStringUsingEncoding:NSUTF8StringEncoding], &st) == 0) {        return st.st_size;    }    return 0;}

11 文件夹大小

-(long long)folderSize{    const char *folderPath = [self cStringUsingEncoding:NSUTF8StringEncoding];    long long folderSize = 0;    DIR* dir = opendir(folderPath);    if (dir == NULL) {        return 0;    }    struct dirent* child;    while ((child = readdir(dir)) != NULL) {        if (child->d_type == DT_DIR            && (child->d_name[0] == '.' && child->d_name[1] == 0)) {            continue;        }                if (child->d_type == DT_DIR            && (child->d_name[0] == '.' && child->d_name[1] == '.' && child->d_name[2] == 0)) {            continue;        }                int folderPathLength = strlen(folderPath);        char childPath[1024];        stpcpy(childPath, folderPath);        if (folderPath[folderPathLength - 1] != '/'){            childPath[folderPathLength] = '/';            folderPathLength++;        }                stpcpy(childPath + folderPathLength, child->d_name);        childPath[folderPathLength + child->d_namlen] = 0;        if (child->d_type == DT_DIR){            folderSize += [[NSString stringWithFormat:@"%s",childPath] fileSize];            struct stat st;            if (lstat(childPath, &st) == 0) {                folderSize += st.st_size;            }        } else if (child->d_type == DT_REG || child->d_type == DT_LNK){            struct stat st;            if (lstat(childPath, &st) == 0) {                folderSize += st.st_size;            }        }    }        return folderSize;}


0 0