IOS development: How to take screenshots and how to merge multiple images...

Source: Internet
Author: User

How to integrate and multiple images in iOS development...

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. Merge multiple images

-(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. Streaming Media

Convert audio and image to base64Binary

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

{

// Base64binary (Streaming Media)

Static char encodingTable [64] = {

'A', 'B', 'C', 'D', 'E', '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', '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];

}

The above method is called here

+ (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

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.