88 lines
2.8 KiB
Objective-C
88 lines
2.8 KiB
Objective-C
|
|
//
|
|
// GIGaFileManager.m
|
|
// GIGA
|
|
//
|
|
// Created by lianxiang on 2018/9/18.
|
|
// Copyright © 2018年 com.giga.ios. All rights reserved.
|
|
//
|
|
|
|
#import "GIGaFileManager.h"
|
|
|
|
@implementation GIGaFileManager
|
|
|
|
+(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];
|
|
|
|
}
|
|
|
|
+ (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;
|
|
|
|
}
|
|
|
|
@end
|