UIimage和NSString的转换

UIImage与字符串互转
//图片转字符串

 -(NSString *)UIImageToBase64Str:(UIImage *) image{
  NSData *data = UIImageJPEGRepresentation(image, 1.0f);
  NSString *encodedImageStr = [data 
  base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
  return encodedImageStr;
  }

//字符串转图片

-(UIImage *)Base64StrToUIImage:(NSString *)_encodedImageStr{
   NSData *_decodedImageData = [[NSData alloc] 
   initWithBase64Encoding:_encodedImageStr];
   UIImage *_decodedImage = [UIImage imageWithData:_decodedImageData];
    return _decodedImage;
    }

截图

 // 1. 开启一个与图片相关的图形上下文
 UIGraphicsBeginImageContextWithOptions(self.view.bounds.size,NO,0.0);

 // 2. 获取当前图形上下文
  CGContextRef ctx = UIGraphicsGetCurrentContext();

 // 3. 获取需要截取的view的layer
  [self.view.layer renderInContext:ctx];

   // 4. 从当前上下文中获取图片
   UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

   // 5. 关闭图形上下文
    UIGraphicsEndImageContext();

   // 6. 把图片保存到相册
   UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);

这样可以通过把图片转换为字符串传输。。。。

你可能感兴趣的:(UIimage和NSString的转换)