MD5
2016-01-23 10:08
405 查看
---------------------------------图片稀释类-----------------------------------
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface ReduceImageRatio :
NSObject
+ (UIImage *)imageWithMaxSide:(CGFloat)length sourceImage:(UIImage *)image;
@end
#import "ReduceImageRatio.h"
@implementation ReduceImageRatio
+ (UIImage *)imageWithMaxSide:(CGFloat)length sourceImage:(UIImage *)image{
CGFloat scale = [[UIScreen
mainScreen] scale];
CGSize imgSize =
GGSSizeReduce(image.size, length);
UIImage *img =
nil;
UIGraphicsBeginImageContextWithOptions(imgSize,
YES, scale);
// 创建一个 bitmap context
[image drawInRect:CGRectMake(0,
0, imgSize.width, imgSize.height)
blendMode:kCGBlendModeNormal
alpha:1.0];
// 将图片绘制到当前的 context
上
img = UIGraphicsGetImageFromCurrentImageContext();
// 从当前 context
中获取刚绘制的图片
UIGraphicsEndImageContext();
return img;
}
static CGSize GGSSizeReduce(CGSize size,
CGFloat limit) //
按比例减少尺寸
{
CGFloat max =
MAX(size.width, size.height);
if (max < limit) {
return size;
}
CGSize imgSize;
CGFloat ratio = size.height / size.width;
if (size.width > size.height) {
imgSize = CGSizeMake(limit, limit*ratio);
} else {
imgSize = CGSizeMake(limit/ratio, limit);
}
return imgSize;
}
@end
---------------------------------MD5类-----------------------------------
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface Md5 : NSObject
+ (NSString *) md5:(NSString *)str;
@end
#import "Md5.h"
#import <CommonCrypto/CommonDigest.h>
@implementation Md5
+ (NSString *) md5:(NSString *)str
{
const char *cStr = [str
UTF8String];
unsigned char result[16];
CC_MD5( cStr, (uint32_t)strlen(cStr), result );
return [NSString
stringWithFormat:@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
result[0], result[1], result[2], result[3],
result[4], result[5], result[6], result[7],
result[8], result[9], result[10], result[11],
result[12], result[13], result[14], result[15]
];
}
@end
#import <Foundation/Foundation.h>
#import <CommonCrypto/CommonDigest.h>
#import <UIKit/UIKit.h>
@interface CreateMD5WithFile : NSObject
+(NSString *)createMD5WithFile:(NSString *)string;
+(NSString*)fileMD5:(NSData *)data;
@end
#import "CreateMD5WithFile.h"
#import "Md5.h"
@implementation CreateMD5WithFile
+(NSString *)createMD5WithFile:(NSData *)imageData
{
NSString * string = [[NSString alloc] initWithData:imageData encoding:NSUTF8StringEncoding];
return [Md5 md5:string];
}
+(NSString*)fileMD5:(NSData *)data
{
NSString * dataPath = NSTemporaryDirectory();
//将图片归档
[data writeToFile:[dataPath stringByAppendingPathComponent:@"dataForImage.tmp"] atomically:YES];
//
NSFileHandle *handle = [NSFileHandle fileHandleForReadingAtPath:[dataPath stringByAppendingPathComponent:@"dataForImage.tmp"]];
if( handle== nil ) return @"ERROR
GETTING FILE MD5"; // file didn't exist
CC_MD5_CTX md5;
CC_MD5_Init(&md5);
BOOL done = NO;
while(!done)
{
NSData* fileData = [handle readDataOfLength: 1000 ];
CC_MD5_Update(&md5, [fileData bytes], [fileData length]);
if( [fileData length] == 0 ) done = YES;
}
unsigned char digest[CC_MD5_DIGEST_LENGTH];
CC_MD5_Final(digest, &md5);
NSString* s = [NSString stringWithFormat: @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
digest[0], digest[1],
digest[2], digest[3],
digest[4], digest[5],
digest[6], digest[7],
digest[8], digest[9],
digest[10], digest[11],
digest[12], digest[13],
digest[14], digest[15]];
return s;
}
@end
---------------------------------使用-----------------------------------
//
// ViewController.m
// FXMD5
//
// Created by Floating_SH on 16/1/6.
// Copyright © 2016年 SH. All rights reserved.
//
#import "ViewController.h"
#import "Md5.h"
#import "CreateMD5WithFile.h"
#import "ReduceImageRatio.h"
@interface
ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super
viewDidLoad];
// 1.根据一个字符串生成一个md5码,
NSString *string1 =
@"http://baidu.com";
NSLog(@"%@",[Md5
md5:string1]);
// 23901b9dc3d84176eb001d386a96be9e
系统MD5
// 23901B9DC3D84176EB001D386A96BE9E
生成MD5
// 2.对一个文件求md5
UIImage *image = [UIImage
imageNamed:@"1"];
//
将图片变小
UIImage *image_xs = [ReduceImageRatio
imageWithMaxSide:200
sourceImage:image];
//
将图片写入沙盒
// (1)归档
NSData *data =
UIImagePNGRepresentation(image_xs);
// (2)写入沙盒
NSString *DocumentPath =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES)[0];
NSString *filePath = [DocumentPath
stringByAppendingPathComponent:@"1.png"];
NSLog(@"%@",filePath);
[data writeToFile:filePath
atomically:YES];
//
对图片求md5
NSLog(@"%@",[CreateMD5WithFile
fileMD5:data]);
}
- (void)didReceiveMemoryWarning {
[super
didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface ReduceImageRatio :
NSObject
+ (UIImage *)imageWithMaxSide:(CGFloat)length sourceImage:(UIImage *)image;
@end
#import "ReduceImageRatio.h"
@implementation ReduceImageRatio
+ (UIImage *)imageWithMaxSide:(CGFloat)length sourceImage:(UIImage *)image{
CGFloat scale = [[UIScreen
mainScreen] scale];
CGSize imgSize =
GGSSizeReduce(image.size, length);
UIImage *img =
nil;
UIGraphicsBeginImageContextWithOptions(imgSize,
YES, scale);
// 创建一个 bitmap context
[image drawInRect:CGRectMake(0,
0, imgSize.width, imgSize.height)
blendMode:kCGBlendModeNormal
alpha:1.0];
// 将图片绘制到当前的 context
上
img = UIGraphicsGetImageFromCurrentImageContext();
// 从当前 context
中获取刚绘制的图片
UIGraphicsEndImageContext();
return img;
}
static CGSize GGSSizeReduce(CGSize size,
CGFloat limit) //
按比例减少尺寸
{
CGFloat max =
MAX(size.width, size.height);
if (max < limit) {
return size;
}
CGSize imgSize;
CGFloat ratio = size.height / size.width;
if (size.width > size.height) {
imgSize = CGSizeMake(limit, limit*ratio);
} else {
imgSize = CGSizeMake(limit/ratio, limit);
}
return imgSize;
}
@end
---------------------------------MD5类-----------------------------------
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface Md5 : NSObject
+ (NSString *) md5:(NSString *)str;
@end
#import "Md5.h"
#import <CommonCrypto/CommonDigest.h>
@implementation Md5
+ (NSString *) md5:(NSString *)str
{
const char *cStr = [str
UTF8String];
unsigned char result[16];
CC_MD5( cStr, (uint32_t)strlen(cStr), result );
return [NSString
stringWithFormat:@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
result[0], result[1], result[2], result[3],
result[4], result[5], result[6], result[7],
result[8], result[9], result[10], result[11],
result[12], result[13], result[14], result[15]
];
}
@end
#import <Foundation/Foundation.h>
#import <CommonCrypto/CommonDigest.h>
#import <UIKit/UIKit.h>
@interface CreateMD5WithFile : NSObject
+(NSString *)createMD5WithFile:(NSString *)string;
+(NSString*)fileMD5:(NSData *)data;
@end
#import "CreateMD5WithFile.h"
#import "Md5.h"
@implementation CreateMD5WithFile
+(NSString *)createMD5WithFile:(NSData *)imageData
{
NSString * string = [[NSString alloc] initWithData:imageData encoding:NSUTF8StringEncoding];
return [Md5 md5:string];
}
+(NSString*)fileMD5:(NSData *)data
{
NSString * dataPath = NSTemporaryDirectory();
//将图片归档
[data writeToFile:[dataPath stringByAppendingPathComponent:@"dataForImage.tmp"] atomically:YES];
//
NSFileHandle *handle = [NSFileHandle fileHandleForReadingAtPath:[dataPath stringByAppendingPathComponent:@"dataForImage.tmp"]];
if( handle== nil ) return @"ERROR
GETTING FILE MD5"; // file didn't exist
CC_MD5_CTX md5;
CC_MD5_Init(&md5);
BOOL done = NO;
while(!done)
{
NSData* fileData = [handle readDataOfLength: 1000 ];
CC_MD5_Update(&md5, [fileData bytes], [fileData length]);
if( [fileData length] == 0 ) done = YES;
}
unsigned char digest[CC_MD5_DIGEST_LENGTH];
CC_MD5_Final(digest, &md5);
NSString* s = [NSString stringWithFormat: @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
digest[0], digest[1],
digest[2], digest[3],
digest[4], digest[5],
digest[6], digest[7],
digest[8], digest[9],
digest[10], digest[11],
digest[12], digest[13],
digest[14], digest[15]];
return s;
}
@end
---------------------------------使用-----------------------------------
//
// ViewController.m
// FXMD5
//
// Created by Floating_SH on 16/1/6.
// Copyright © 2016年 SH. All rights reserved.
//
#import "ViewController.h"
#import "Md5.h"
#import "CreateMD5WithFile.h"
#import "ReduceImageRatio.h"
@interface
ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super
viewDidLoad];
// 1.根据一个字符串生成一个md5码,
NSString *string1 =
@"http://baidu.com";
NSLog(@"%@",[Md5
md5:string1]);
// 23901b9dc3d84176eb001d386a96be9e
系统MD5
// 23901B9DC3D84176EB001D386A96BE9E
生成MD5
// 2.对一个文件求md5
UIImage *image = [UIImage
imageNamed:@"1"];
//
将图片变小
UIImage *image_xs = [ReduceImageRatio
imageWithMaxSide:200
sourceImage:image];
//
将图片写入沙盒
// (1)归档
NSData *data =
UIImagePNGRepresentation(image_xs);
// (2)写入沙盒
NSString *DocumentPath =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES)[0];
NSString *filePath = [DocumentPath
stringByAppendingPathComponent:@"1.png"];
NSLog(@"%@",filePath);
[data writeToFile:filePath
atomically:YES];
//
对图片求md5
NSLog(@"%@",[CreateMD5WithFile
fileMD5:data]);
}
- (void)didReceiveMemoryWarning {
[super
didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
相关文章推荐
- 人人必知的10个jQuery小技巧
- 数据结构(C实现)------- 单链表
- android camera HAL 错误数据流处理
- DL动态加载框架技术文档
- CentOS 7 下安装 LEMP 服务(nginx、MariaDB/MySQL 和 php)
- 极光推送
- [ssh新闻发布系统第二天] 读取新闻
- MySQL - 建库、建表、查询
- Atom编辑器中文汉化方法
- linux下的hiredis的安装和使用(一)
- 有关拍照并获取图片显示出来的问题
- AutoCAD.Net/C#.Net QQ群:193522571 从已有A.DWG中复制块BLK到新DWG中的方法
- 生活中的幽默!有趣的纸上立体插画艺术
- iOS UITableView全面解析 (转载)
- Nginx性能优化
- 字,字长,字节
- 理论: 二分查找(1):基础样例
- 使用history.replaceState 控制微信返回
- 互联网协议入门(一)
- NopCommerce添加新功能“类型属性”【新增实体类】