iOS開發之如何截圖and如何合并多張圖片等...

來源:互聯網
上載者:User

   1、UIView截圖

  -(UIImage*)captureView:(UIView *)theView{

  CGRect rect = theView.frame;

  if ([theView isKindOfClass:[UIScrollView class]]) {

  rect.size = ((UIScrollView *)theView).contentSize;

  }

  UIGraphicsBeginImageContext(rect.size);

  CGContextRef context = UIGraphicsGetCurrentContext();

  [theView.layer renderInContext:context];

  UIImage *img = UIGraphicsGetImageFromCurrentImageContext();

  UIGraphicsEndImageContext();

  return img;

  }

  2、UIScrollview截圖

  - (UIImage *)captureScrollView:(UIScrollView *)scrollView{

  UIImage* image = nil;

  UIGraphicsBeginImageContext(scrollView.contentSize);

  {

  CGPoint savedContentOffset = scrollView.contentOffset;

  CGRect savedFrame = scrollView.frame;

  scrollView.contentOffset = CGPointZero;

  scrollView.frame = CGRectMake(0, 0, scrollView.contentSize.width, scrollView.contentSize.height);

  [scrollView.layer renderInContext: UIGraphicsGetCurrentContext()];

  image = UIGraphicsGetImageFromCurrentImageContext();

  scrollView.contentOffset = savedContentOffset;

  scrollView.frame = savedFrame;

  }

  UIGraphicsEndImageContext();

  if (image != nil) {

  return image;

  }

  return nil;

  }

  3、合并多張圖片

  - (UIImage *)composeWithHeader:(UIImage *)header content:(UIImage *)content footer:(UIImage *)footer{

  CGSize size = CGSizeMake(content.size.width, header.size.height +content.size.height +footer.size.height);

  UIGraphicsBeginImageContext(size);

  [header drawInRect:CGRectMake(0,

  0,

  header.size.width,

  header.size.height)];

  [content drawInRect:CGRectMake(0,

  header.size.height,

  content.size.width,

  content.size.height)];

  [footer drawInRect:CGRectMake(0,

  header.size.height+content.size.height,

  footer.size.width,

  footer.size.height)];

  UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

  UIGraphicsEndImageContext();

  return image;

  }

  4、流媒體

  將音頻、圖片轉成base64Binary

  +(NSData *)base64Encoded:(NSData *)data

  {

  //base64binary(流媒體)

  static char encodingTable[64] = {

  'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',

  'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f',

  'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',

  'w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/' };

  const unsigned char *bytes = [data bytes];

  NSMutableString *result = [NSMutableString stringWithCapacity:[data length]];

  unsigned long ixtext = 0;

  unsigned long lentext = [data length];

  long ctremaining = 0;

  unsigned char inbuf[3], outbuf[4];

  unsigned short i = 0;

  unsigned short charsonline = 0, ctcopy = 0;

  unsigned long ix = 0;

  while( YES )

  {

  ctremaining = lentext - ixtext;

  if( ctremaining <= 0 ) break;

  for( i = 0; i < 3; i++ ) {

  ix = ixtext + i;

  if( ix < lentext ) inbuf[i] = bytes[ix];

  else inbuf [i] = 0;

  }

  outbuf [0] = (inbuf [0] & 0xFC) >> 2;

  outbuf [1] = ((inbuf [0] & 0x03) << 4) | ((inbuf [1] & 0xF0) >> 4);

  outbuf [2] = ((inbuf [1] & 0x0F) << 2) | ((inbuf [2] & 0xC0) >> 6);

  outbuf [3] = inbuf [2] & 0x3F;

  ctcopy = 4;

  switch( ctremaining )

  {

  case 1:

  ctcopy = 2;

  break;

  case 2:

  ctcopy = 3;

  break;

  }

  for( i = 0; i < ctcopy; i++ )

  [result appendFormat:@"%c", encodingTable[outbuf[i]]];

  for( i = ctcopy; i < 4; i++ )

  [result appendString:@"="];

  ixtext += 3;

  charsonline += 4;

  }

  return [result dataUsingEncoding:NSUTF8StringEncoding];

  }

  這裡調用上面的方法

  +(NSString *)loadingFile:(NSURL *)filepath

  {

  NSData * data = [[NSData alloc]initWithContentsOfURL:filepath];

  NSData * base64Data = [self base64Encoded:data];

  NSString * strBase64 = [[NSString alloc]initWithData:base64Data encoding:NSASCIIStringEncoding];

  return strBase64;

  //you can upload this base64Data to server now

  }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.