1
UIGraphicsBeginImageContextWithOptions(pageView.page.bounds.size,YES, zoomScale);
[pageView.page.layerrenderInContext:UIGraphicsGetCurrentContext()];
UIImage*uiImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
2.
[objc]view plaincopy
- (UIImage*) glToUIImage {
DWScrollView*pageView = [selfgetActivePageView];
pageView.page.backgroundColor= [UIColorclearColor];
// self.backgroundColor=[UIColor clearColor];
NSInteger myDataLength =3320*3308*4;
// allocate array and read pixels into it.
GLubyte*buffer = (GLubyte*) malloc(myDataLength);
glReadPixels(0,0,320,308, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
// gl renders "upside down" so swap top to bottom into new array.
// there's gotta be a better way, but this works.
GLubyte*buffer2= (GLubyte*) malloc(myDataLength);
for(inty =0; y <308; y++)
{
for(intx =0; x <3320*4; x++)
{
if(buffer[y*44*320+x]==0)
buffer2[(307- y) *3320*4+x]=1;
else
buffer2[(307- y) *3320*4+x] = buffer[y*44*320+x];
}
}
// make data provider with data.
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer2, myDataLength,NULL);
// prep the ingredients
intbitsPerComponent =8;
intbitsPerPixel =32;
intbytesPerRow =44*320;
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
// make the cgimage
CGImageRef
imageRef = CGImageCreate(320,308, bitsPerComponent, bitsPerPixel,
bytesPerRow, colorSpaceRef, bitmapInfo, provider,NULL,NO,
renderingIntent);
// then make the uiimage from that
UIImage*myImage = [UIImageimageWithCGImage:imageRef];
UIImageWriteToSavedPhotosAlbum(myImage,nil,nil,nil);
returnmyImage;
}
3.
[objc]view plaincopy
// get screen
- (void)grabScreen {
unsignedcharbuffer[320*480*4];
glReadPixels(0,0,320,480,GL_RGBA,GL_UNSIGNED_BYTE,&buffer);
CGDataProviderRef ref = CGDataProviderCreateWithData(NULL, &buffer,320*480*4,NULL);
CGImageRefiref =CGImageCreate(320,480,8,32,320*4,CGColorSpaceCreateDeviceRGB(),kCGBitmapByteOrderDefault,ref,NULL,true,kCGRenderingIntentDefault);
CGFloat width = CGImageGetWidth(iref);
CGFloat height = CGImageGetHeight(iref);
size_tlength = width*height*4;
uint32_t*pixels = (uint32_t*)malloc(length);
CGContextRef
context = CGBitmapContextCreate(pixels, width, height,8,320*4,
CGImageGetColorSpace(iref), kCGImageAlphaLast |
kCGBitmapByteOrder32Big);
CGContextTranslateCTM(context,0.0, height);
CGContextScaleCTM(context,1.0, -1.0);
CGContextDrawImage(context, CGRectMake(0.0,0.0, width, height), iref);
CGImageRef outputRef = CGBitmapContextCreateImage(context);
UIImage*outputImage = [UIImageimageWithCGImage:outputRef];
UIImageWriteToSavedPhotosAlbum(outputImage,nil,nil,nil);
CGContextRelease(context);
CGImageRelease(iref);
CGDataProviderRelease(ref);
}
4.
[objc]view plaincopy
CGImageRef UIGetScreenImage();
voidSaveScreenImage(NSString*path)
{
NSAutoreleasePool*pool = [[NSAutoreleasePoolalloc]init];
CGImageRef cgImage = UIGetScreenImage();
voidvoid*imageBytes =NULL;
if(cgImage ==NULL) {
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
imageBytes = malloc(3320*4480*4);
CGContextRef
context = CGBitmapContextCreate(imageBytes,320,480,8,3320*4,
colorspace, kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorspace);
for(UIWindow*window in [[UIApplicationsharedApplication]windows]) {
CGRect bounds = [windowbounds];
CALayer*layer = [windowlayer];
CGContextSaveGState(context);
if([layercontentsAreFlipped]) {
CGContextTranslateCTM(context,0.0f, bounds.size.height);
CGContextScaleCTM(context,1.0f, -1.0f);
}
[layerrenderInContext:(CGContextRef)context];
CGContextRestoreGState(context);
}
cgImage = CGBitmapContextCreateImage(context);
CGContextRelease(context);
}
NSData*pngData = UIImagePNGRepresentation([UIImageimageWithCGImage:cgImage]);
CGImageRelease(cgImage);
if(imageBytes)
free(imageBytes);
[pngDatawriteToFile:pathatomically:YES];
[poolrelease];
}
5.
[objc]view plaincopy
+ (UIImage*)imageWithScreenContents
{
CGImageRef cgScreen = UIGetScreenImage();
if(cgScreen) {
UIImage*result = [UIImageimageWithCGImage:cgScreen];
CGImageRelease(cgScreen);
returnresult;
}
returnnil;
}
在程序中如何把两张图片合成为一张图片
- (UIImage*)addImage:(UIImage*)image1toImage:(UIImage*)image2{
UIGraphicsBeginImageContext(image1.size);
// Draw image1
[image1drawInRect:CGRectMake(0,0, image1.size.width, image1.size.height)];
// Draw image2
[image2drawInRect:CGRectMake(0,0, image2.size.width, image2.size.height)];
UIImage*resultingImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
returnresultingImage;
}