GiGaMaskTime/GIGA/Common/GiGaUserDB/GIGaUserFileHelper.m

189 lines
6.5 KiB
Objective-C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// GIGaUserFileHelper.m
// GIGA
//
// Created by lianxiang on 2018/9/21.
// Copyright © 2018年 com.giga.ios. All rights reserved.
//
#import "GIGaUserFileHelper.h"
@implementation GIGaUserFileHelper
+(void)savaUserAvatorWith:(NSString *)userId image:(UIImage *)image{
if (!userId) {
GILog(@"userID nil !!!");
return;
}
NSData*imageData=UIImageJPEGRepresentation(image,1.0f);
NSString *fullPath=[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:userId];
[imageData writeToFile:fullPath atomically:YES];
}
+(void)savaShareImange:(NSString *)fileName image:(UIImage *)image{
NSData*imageData=UIImageJPEGRepresentation(image,1.0f);
NSString *fullPath=[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:fileName];
[imageData writeToFile:fullPath atomically:YES];
}
+(void)delectShareImage:(NSString *)imageName{
//删除文件夹及文件级内的文件:
NSString *fullPath=[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:imageName];
NSFileManager *fileManager = [NSFileManager defaultManager];[fileManager removeItemAtPath:fullPath error:nil];
}
+(UIImage *)getUserShareImage:(NSString *)imageName{
NSString*fullpath=[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:imageName];
UIImage*saveImage=[[UIImage alloc]initWithContentsOfFile:fullpath];
if (!saveImage) {
saveImage = [UIImage imageNamed:@"sharebg_1"];
}
return saveImage;
}
+ (UIImage *)getUserAvatorWith:(NSString *)userId{
if (!userId) {
GILog(@"userID nil !!!");
return nil;
}
NSString*fullpath=[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:userId];
UIImage*saveImage=[[UIImage alloc]initWithContentsOfFile:fullpath];
if (!saveImage) {
saveImage = [UIImage imageNamed:@"img_circle_avatar"];
}
return saveImage;
}
// 改变图像的尺寸,方便上传服务器
+ (UIImage *) scaleFromImage: (UIImage *) image toSize: (CGSize) size
{
UIGraphicsBeginImageContext(size);
[image drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
//保持原来的长宽比,生成一个缩略图
+ (UIImage *)thumbnailWithImageWithoutScale:(UIImage *)image size:(CGSize)asize
{
UIImage *newimage;
if (nil == image) {
newimage = nil;
}
else{
CGSize oldsize = image.size;
CGRect rect;
if (asize.width/asize.height > oldsize.width/oldsize.height) {
rect.size.width = asize.width*oldsize.height/oldsize.width;
// rect.size.height = asize.height*oldsize.height/oldsize.width;
rect.size.height = asize.width*oldsize.height/oldsize.width;
rect.origin.x = (asize.width - rect.size.width)/2;
rect.origin.y = 0;
}
else{
//rect.size.width = asize.width;
rect.size.width = asize.height*oldsize.width/oldsize.height;
rect.size.height = asize.height*oldsize.width/oldsize.height;
rect.origin.x = 0;
rect.origin.y = (asize.height - rect.size.height)/2;
}
UIGraphicsBeginImageContext(asize);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]);
UIRectFill(CGRectMake(0, 0, asize.width, asize.height));//clear background
[image drawInRect:rect];
newimage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
return newimage;
}
+(UIImage *)zipScaleWithImage:(UIImage *)sourceImage{
//进行图像尺寸的压缩
CGSize imageSize = sourceImage.size;
//取出要压缩的image尺寸
CGFloat width = imageSize.width;
//图片宽度
CGFloat height = imageSize.height;
//图片高度 //1.宽高大于1280(宽高比不按照2来算按照1来算)
if (width>1280||height>1280) { if (width>height) { CGFloat scale = height/width; width = 1280; height = width*scale;
}else{ CGFloat scale = width/height;
height = 1280; width = height*scale;
}
//2.宽大于1280高小于1280
}else if(width>1280||height<1280){
CGFloat scale = height/width;
width = 1280;
height = width*scale;
//3.宽小于1280高大于1280
}else if(width<1280||height>1280){
CGFloat scale = width/height; height = 1280; width = height*scale;
//4.宽高都小于1280
}else{
}
//进行尺寸重绘
UIGraphicsBeginImageContext(CGSizeMake(width, height));
[sourceImage drawInRect:CGRectMake(0,0,width,height)]; UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext();
return newImage;
}
+(void)saveUserFeedbackImag:(NSString *)imagename image:(UIImage *)image
{
NSData*imageData=UIImagePNGRepresentation(image);
NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *savePath = nil;
savePath = [NSString stringWithFormat:@"%@/gigafee/%@",rootPath,imagename];
NSFileManager *manager = [NSFileManager defaultManager];
if(![manager fileExistsAtPath:savePath]){
[manager createDirectoryAtPath:savePath withIntermediateDirectories:YES attributes:nil error:nil];
}
[imageData writeToFile:[NSString stringWithFormat:@"%@/%@.png",savePath,imagename] atomically:YES];
}
+(UIImage *)getUserFeedbackImag:(NSString *)imageName{
NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = nil;
filePath = [NSString stringWithFormat:@"%@/gigafee/%@/%@.png",rootPath,imageName,imageName];
NSLog(@"获取Path:%@",filePath);
UIImage *Img = [UIImage imageWithContentsOfFile:filePath];
return Img;
}
+(void)delectAllfeedimages{
NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = nil;
filePath = [NSString stringWithFormat:@"%@/gigafee/",rootPath];
NSFileManager *fileManager = [NSFileManager defaultManager];[fileManager removeItemAtPath:filePath error:nil];
}
@end