Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2014
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. - (UIImage *)testChangeImageToBase64String
  2. {
  3. UIImage *processedImage = [UIImage imageNamed:@"myFile.jpg"];
  4.  
  5. // UIImage to unsigned char *
  6. CGImageRef imageRef = processedImage.CGImage;
  7. NSData *data = (NSData *) CFBridgingRelease(CGDataProviderCopyData(CGImageGetDataProvider(imageRef)));
  8.  
  9. // encode data to Base64 NSString
  10. NSString *base64EncodedDataString = [data base64EncodedStringWithOptions:0];
  11. // create encoded std::string
  12. std::string encoded([base64EncodedDataString UTF8String]);
  13.  
  14.  
  15. // ***************************************************************************
  16. // This is where we call the server method and receive the bytes in a std::string
  17. std::string received = encoded;
  18. // ***************************************************************************
  19.  
  20.  
  21. // get Base64 encoded std::string into NSString
  22. NSString *base64EncodedCstring = [NSString stringWithCString:encoded.c_str() encoding:[NSString defaultCStringEncoding]];
  23.  
  24. // NSData from the Base64 encoded std::string
  25. NSData *nsdataFromBase64String = [[NSData alloc]initWithBase64EncodedString:base64EncodedCstring options:0];
  26.  
  27. CGImageRef base64ImageRef = [newCGImageRefFromString:nsdataFromBase64String];
  28.  
  29. UIImage *imageFromImageRef = [UIImage imageWithCGImage: base64ImageRef];
  30.  
  31. return newImage;
  32. }
  33.  
  34. UIImage *newImage = [[UIImage alloc] initWithData:nsdataFromBase64String];
  35.  
  36. // NOTE: have not tested if this even compiles -- consider it pseudocode.
  37.  
  38. CGImageRef image;
  39. CFDataRef bridgedData;
  40. CGDataProviderRef dataProvider;
  41. CGColorSpaceRef colorSpace;
  42. CGBitmapInfo infoFlags = kCGImageAlphaFirst; // ARGB
  43.  
  44. // Get a color space
  45. colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
  46. // Assuming the decoded data is only pixel data
  47. bridgedData = (__bridge CFDataRef)decodedData;
  48. dataProvider = CGDataProviderCreateWithCFData(bridgedData);
  49.  
  50. // Given size_t width, height which you should already have somehow
  51. image = CGImageCreate(
  52. width, height, /* bpc */ 8, /* bpp */ 32, /* pitch */ width * 4,
  53. colorSpace, infoFlags,
  54. dataProvider, /* decode array */ NULL, /* interpolate? */ TRUE,
  55. kCGRenderingIntentDefault /* adjust intent according to use */
  56. );
  57.  
  58. // Release things the image took ownership of.
  59. CGDataProviderRelease(dataProvider);
  60. CGColorSpaceRelease(colorSpace);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement