Photo.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. //
  2. // Photo.m
  3. // Components
  4. // 照片处理对象
  5. // Created by Liu Yang on 10-9-15.
  6. // Copyright 2010 __MyCompanyName__. All rights reserved.
  7. //
  8. #import "Photo.h"
  9. #import "AppDelegate.h"
  10. static const char encodingTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  11. @implementation NSData (MBBase64)
  12. + (id)dataWithBase64EncodedString:(NSString *)string;
  13. {
  14. if (string == nil)
  15. // [NSException raise:NSInvalidArgumentException format:@""];
  16. return nil;
  17. if ([string length] == 0)
  18. return [NSData data];
  19. static char *decodingTable = NULL;
  20. if (decodingTable == NULL)
  21. {
  22. decodingTable = malloc(256);
  23. if (decodingTable == NULL)
  24. return nil;
  25. memset(decodingTable, CHAR_MAX, 256);
  26. NSUInteger i;
  27. for (i = 0; i < 64; i++)
  28. decodingTable[(short)encodingTable[i]] = i;
  29. }
  30. const char *characters = [string cStringUsingEncoding:NSASCIIStringEncoding];
  31. if (characters == NULL) // Not an ASCII string!
  32. return nil;
  33. char *bytes = malloc((([string length] + 3) / 4) * 3);
  34. if (bytes == NULL)
  35. return nil;
  36. NSUInteger length = 0;
  37. NSUInteger i = 0;
  38. while (YES)
  39. {
  40. char buffer[4];
  41. short bufferLength;
  42. for (bufferLength = 0; bufferLength < 4; i++)
  43. {
  44. if (characters[i] == '\0')
  45. break;
  46. if (isspace(characters[i]) || characters[i] == '=')
  47. continue;
  48. buffer[bufferLength] = decodingTable[(short)characters[i]];
  49. if (buffer[bufferLength++] == CHAR_MAX) // Illegal character!
  50. {
  51. free(bytes);
  52. return nil;
  53. }
  54. }
  55. if (bufferLength == 0)
  56. break;
  57. if (bufferLength == 1) // At least two characters are needed to produce one byte!
  58. {
  59. free(bytes);
  60. return nil;
  61. }
  62. // Decode the characters in the buffer to bytes.
  63. bytes[length++] = (buffer[0] << 2) | (buffer[1] >> 4);
  64. if (bufferLength > 2)
  65. bytes[length++] = (buffer[1] << 4) | (buffer[2] >> 2);
  66. if (bufferLength > 3)
  67. bytes[length++] = (buffer[2] << 6) | buffer[3];
  68. }
  69. realloc(bytes, length);
  70. return [NSData dataWithBytesNoCopy:bytes length:length];
  71. // return [[NSData alloc] initWithBytesNoCopy:bytes length:length];
  72. }
  73. - (NSString *)base64Encoding;
  74. {
  75. if ([self length] == 0)
  76. return @"";
  77. char *characters = malloc((([self length] + 2) / 3) * 4);
  78. if (characters == NULL)
  79. return nil;
  80. NSUInteger length = 0;
  81. NSUInteger i = 0;
  82. while (i < [self length])
  83. {
  84. char buffer[3] = {0,0,0};
  85. short bufferLength = 0;
  86. while (bufferLength < 3 && i < [self length])
  87. buffer[bufferLength++] = ((char *)[self bytes])[i++];
  88. // Encode the bytes in the buffer to four characters, including padding "=" characters if necessary.
  89. characters[length++] = encodingTable[(buffer[0] & 0xFC) >> 2];
  90. characters[length++] = encodingTable[((buffer[0] & 0x03) << 4) | ((buffer[1] & 0xF0) >> 4)];
  91. if (bufferLength > 1)
  92. characters[length++] = encodingTable[((buffer[1] & 0x0F) << 2) | ((buffer[2] & 0xC0) >> 6)];
  93. else characters[length++] = '=';
  94. if (bufferLength > 2)
  95. characters[length++] = encodingTable[buffer[2] & 0x3F];
  96. else characters[length++] = '=';
  97. }
  98. return [[NSString alloc] initWithBytesNoCopy:characters length:length encoding:NSASCIIStringEncoding freeWhenDone:YES];
  99. }
  100. @end
  101. @implementation Photo
  102. #pragma mark -
  103. #pragma mark 内部方法
  104. +(NSString *) image2String:(UIImage *)image{
  105. NSMutableDictionary *systeminfo = [NSMutableDictionary dictionaryWithContentsOfFile:[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"systeminfo"]];
  106. float o = 0.1;
  107. if (!image){//如果没有图则不操作
  108. return @"";
  109. }
  110. image = [self scaleImage:image toWidth:image.size.width/3 toHeight:image.size.height/3];
  111. if (systeminfo){//如果有系统设置信息
  112. if ([[systeminfo objectForKey:@"imagesize"] isEqualToString:@"大"]){
  113. o=0.7;
  114. }
  115. if ([[systeminfo objectForKey:@"imagesize"] isEqualToString:@"中"]){
  116. o=0.5;
  117. }
  118. if ([[systeminfo objectForKey:@"imagesize"] isEqualToString:@"小"]){
  119. o=0.2;
  120. }
  121. }
  122. NSData* pictureData = UIImageJPEGRepresentation(image,o);
  123. NSString* pictureDataString = [pictureData base64Encoding];
  124. return pictureDataString;
  125. }
  126. //截取图片
  127. +(NSData *) image2Data:(UIImage *)image isOriginal:(BOOL)isOriginal{
  128. /* NSMutableDictionary *systeminfo = [NSMutableDictionary dictionaryWithContentsOfFile:[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"systeminfo"]];
  129. float o = 0.1;
  130. if (!image){//如果没有图则不操作
  131. return @"";
  132. }
  133. image = [self scaleImage:image toWidth:image.size.width/3 toHeight:image.size.height/3];
  134. if (systeminfo){//如果有系统设置信息
  135. if ([[systeminfo objectForKey:@"imagesize"] isEqualToString:@"大"]){
  136. o=0.7;
  137. }
  138. if ([[systeminfo objectForKey:@"imagesize"] isEqualToString:@"中"]){
  139. o=0.5;
  140. }
  141. if ([[systeminfo objectForKey:@"imagesize"] isEqualToString:@"小"]){
  142. o=0.2;
  143. }
  144. }*/
  145. image = [self scaleImage:image toWidth:image.size.width toHeight:image.size.height];
  146. NSData* pictureData;
  147. if (isOriginal)
  148. pictureData = UIImageJPEGRepresentation(image,1);
  149. else
  150. pictureData = UIImageJPEGRepresentation(image,0.4);
  151. return pictureData;
  152. }
  153. +(UIImage *) string2Image:(NSString *)string{
  154. UIImage *image = [UIImage imageWithData:[NSData dataWithBase64EncodedString:string]];
  155. return image;
  156. }
  157. static void addRoundedRectToPath(CGContextRef context, CGRect rect, float ovalWidth,float ovalHeight){
  158. float fw, fh;
  159. if (ovalWidth == 0 || ovalHeight == 0) {
  160. CGContextAddRect(context, rect);
  161. return;
  162. }
  163. CGContextSaveGState(context);
  164. CGContextTranslateCTM(context, CGRectGetMinX(rect), CGRectGetMinY(rect));
  165. CGContextScaleCTM(context, ovalWidth, ovalHeight);
  166. fw = CGRectGetWidth(rect) / ovalWidth;
  167. fh = CGRectGetHeight(rect) / ovalHeight;
  168. CGContextMoveToPoint(context, fw, fh/2); // Start at lower right corner
  169. CGContextAddArcToPoint(context, fw, fh, fw/2, fh, 1); // Top right corner
  170. CGContextAddArcToPoint(context, 0, fh, 0, fh/2, 1); // Top left corner
  171. CGContextAddArcToPoint(context, 0, 0, fw/2, 0, 1); // Lower left corner
  172. CGContextAddArcToPoint(context, fw, 0, fw, fh/2, 1); // Back to lower right
  173. CGContextClosePath(context);
  174. CGContextRestoreGState(context);
  175. }
  176. + (id) createRoundedRectImage:(UIImage*)image size:(CGSize)size{
  177. // the size of CGContextRef
  178. int w = size.width;
  179. int h = size.height;
  180. UIImage *img = image;
  181. CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
  182. CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
  183. CGRect rect = CGRectMake(0, 0, w, h);
  184. CGContextBeginPath(context);
  185. addRoundedRectToPath(context, rect, 10, 10);
  186. CGContextClosePath(context);
  187. CGContextClip(context);
  188. CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);
  189. CGImageRef imageMasked = CGBitmapContextCreateImage(context);
  190. UIImage *result= [UIImage imageWithCGImage:imageMasked];
  191. CGImageRelease(imageMasked);
  192. CGContextRelease(context);
  193. CGColorSpaceRelease(colorSpace);
  194. return result;
  195. }
  196. +(UIImage *)scaleImage:(UIImage *)image toWidth:(int)toWidth toHeight:(int)toHeight{
  197. int width=0;
  198. int height=0;
  199. int x=0;
  200. int y=0;
  201. if (image.size.width<toWidth){
  202. width = toWidth;
  203. height = image.size.height*(toWidth/image.size.width);
  204. y = (height - toHeight)/2;
  205. }else if (image.size.height<toHeight){
  206. height = toHeight;
  207. width = image.size.width*(toHeight/image.size.height);
  208. x = (width - toWidth)/2;
  209. }else if (image.size.width>toWidth){
  210. width = toWidth;
  211. height = image.size.height*(toWidth/image.size.width);
  212. y = (height - toHeight)/2;
  213. }else if (image.size.height>toHeight){
  214. height = toHeight;
  215. width = image.size.width*(toHeight/image.size.height);
  216. x = (width - toWidth)/2;
  217. }else{
  218. height = toHeight;
  219. width = toWidth;
  220. }
  221. CGSize size = CGSizeMake(width, height);
  222. UIGraphicsBeginImageContext(size);
  223. [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
  224. image = UIGraphicsGetImageFromCurrentImageContext();
  225. UIGraphicsEndImageContext();
  226. CGSize subImageSize = CGSizeMake(toWidth, toHeight);
  227. CGRect subImageRect = CGRectMake(x, y, toWidth, toHeight);
  228. CGImageRef imageRef = image.CGImage;
  229. CGImageRef subImageRef = CGImageCreateWithImageInRect(imageRef, subImageRect);
  230. UIGraphicsBeginImageContext(subImageSize);
  231. CGContextRef context = UIGraphicsGetCurrentContext();
  232. CGContextDrawImage(context, subImageRect, subImageRef);
  233. UIImage* subImage = [UIImage imageWithCGImage:subImageRef];
  234. CGImageRelease(subImageRef);
  235. UIGraphicsEndImageContext();
  236. return subImage;
  237. }
  238. +(NSData *)scaleData:(NSData *)imageData toWidth:(int)toWidth toHeight:(int)toHeight{
  239. UIImage *image = [[UIImage alloc] initWithData:imageData];
  240. int width=0;
  241. int height=0;
  242. int x=0;
  243. int y=0;
  244. if (image.size.width<toWidth){
  245. width = toWidth;
  246. height = image.size.height*(toWidth/image.size.width);
  247. y = (height - toHeight)/2;
  248. }else if (image.size.height<toHeight){
  249. height = toHeight;
  250. width = image.size.width*(toHeight/image.size.height);
  251. x = (width - toWidth)/2;
  252. }else if (image.size.width>toWidth){
  253. width = toWidth;
  254. height = image.size.height*(toWidth/image.size.width);
  255. y = (height - toHeight)/2;
  256. }else if (image.size.height>toHeight){
  257. height = toHeight;
  258. width = image.size.width*(toHeight/image.size.height);
  259. x = (width - toWidth)/2;
  260. }else{
  261. height = toHeight;
  262. width = toWidth;
  263. }
  264. CGSize size = CGSizeMake(width, height);
  265. UIGraphicsBeginImageContext(size);
  266. [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
  267. // [image release];
  268. image = UIGraphicsGetImageFromCurrentImageContext();
  269. UIGraphicsEndImageContext();
  270. CGSize subImageSize = CGSizeMake(toWidth, toHeight);
  271. CGRect subImageRect = CGRectMake(x, y, toWidth, toHeight);
  272. CGImageRef imageRef = image.CGImage;
  273. CGImageRef subImageRef = CGImageCreateWithImageInRect(imageRef, subImageRect);
  274. UIGraphicsBeginImageContext(subImageSize);
  275. CGContextRef context = UIGraphicsGetCurrentContext();
  276. CGContextDrawImage(context, subImageRect, subImageRef);
  277. UIImage* subImage = [UIImage imageWithCGImage:subImageRef];
  278. CGImageRelease(subImageRef);
  279. UIGraphicsEndImageContext();
  280. NSData *data = UIImageJPEGRepresentation(subImage,1.0);
  281. return data;
  282. }
  283. @end