Off Screen bitmap drawing
Off Screen bitmap drawing
- Subject: Off Screen bitmap drawing
- From: Development <email@hidden>
- Date: Mon, 31 May 2010 11:11:29 -0700
Ok... I'm really new to coregraphics and i've run in to a problem i cant seem to figure out.
I'm trying to take page data from a pdf and draw a thumbnail size preview... I'm first drawing this to a UIView, then creating CGImageRef from that and finally converting it to a UIImage.
What I get is a black rectangle of scrambled colors and no discernible resemblance to the page data.
self.backgroundColor = [UIColor whiteColor];
float scale=1.0;
CGRect pageRect = CGPDFPageGetBoxRect(thePage,0);
if (pageRect.size.width > pageRect.size.height) {
scale = self.imgSize/pageRect.size.width;
}
else {
scale = self.imgSize/pageRect.size.height;
}
CGRect myBounds = self.bounds;
myBounds.size.width =pageRect.size.width *scale;
myBounds.size.height = pageRect.size.height *scale;
self.bounds = myBounds;
int bytesPerRow = myBounds.size.width*4;
bytesPerRow += (16 - bytesPerRow);
size_t byteSize = bytesPerRow * myBounds.size.height;
void * bitmapData = malloc(byteSize); ag
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(bitmapData,myBounds.size.width,myBounds.size.height,8,bytesPerRow,colorSpace,kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Little/*kCGImageAlphaPremultipliedFirst*/);
CGContextTranslateCTM(context, 0.0, self.bounds.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextSetPatternPhase(context, CGSizeMake(0,myBounds.size.height));
CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(thePage, kCGPDFCropBox, myBounds, 0, true);
CGContextConcatCTM(context, pdfTransform);
CGContextDrawPDFPage(context, thePage);
CGImageRef testImage = CGBitmapContextCreateImage(context);
thumb = [[UIImageView alloc]initWithImage:[UIImage imageWithCGImage:testImage]];
CGColorSpaceRelease(colorSpace);_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden