Reputation: 141
I have an app which combines two images using the CGContextDrawImage function. Here's my problem. Only one image is appearing while the other does not appear in the ios simulator for the iphone 5 while on the iphone simulator for iphone 3 there's no problem in displaying those images and I don't have any problem. By the way here are the lists of the errors that Xcode is giving me.
//ERRORS
: CGContextRotateCTM: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.
: CGContextDrawImage: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.
: CGBitmapContextCreateImage: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.
Can anyone help me please? Thanks in advance.
Here's my sample code:
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGRect xFirstFrame = m_pUserImgView.frame;
int nWidth = xFirstFrame.size.width;
int nHeight = xFirstFrame.size.height;
CGFloat rScaleX;
CGPoint xOrgCenter;
UIImage* pJustinImage = m_pJustinImgView.image;
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
xOrgCenter = CGPointMake(384, 512);
}
else
if ([self appDelegate].isiPhone5 == YES)
{
xOrgCenter = CGPointMake(160, 284);
}
else
{
xOrgCenter = CGPointMake(160, 240);
}
CGContextRef buf_context = CGBitmapContextCreate(nil, nWidth, nHeight, 8, nWidth * 4, colorSpace, kCGImageAlphaPremultipliedLast);
CGColorSpaceRelease(colorSpace);
CGRect rect = CGRectMake(0,0, nWidth, nHeight);
CGRect xSecRect = m_pJustinImgView.bounds;
UIGraphicsPushContext(buf_context);
CGContextClearRect(buf_context, rect);
CGContextDrawImage(buf_context, rect, m_pUserImgView.image.CGImage);
//Transform Context
CGAffineTransform pTransform = m_pJustinImgView.transform;
CGAffineTransform xRealTrans = CGAffineTransformMake(pTransform.a, pTransform.b, pTransform.c, pTransform.d, pTransform.tx, -pTransform.ty);
CGPoint xOrgPoint = CGPointMake(xOrgCenter.x + pTransform.tx, xOrgCenter.y - pTransform.ty);
CGFloat rDegree = [self CalcRotateAngle:xRealTrans];
CGContextTranslateCTM(buf_context, xOrgPoint.x, xOrgPoint.y);
CGContextRotateCTM(buf_context, -rDegree);
rScaleX = sqrtf(pTransform.a * pTransform.a + pTransform.c * pTransform.c);
CGContextDrawImage(buf_context, CGRectMake(- xSecRect.size.width * rScaleX/2, - xSecRect.size.height * rScaleX/2, xSecRect.size.width * rScaleX , xSecRect.size.height * rScaleX), pJustinImage.CGImage);
UIGraphicsPopContext();
CGImageRef image = CGBitmapContextCreateImage(buf_context);
CGContextRelease(buf_context);
UIImage* pCombImage = [[UIImage alloc] initWithCGImage:image];
CGImageRelease(image);
return pCombImage;
}
//Code for loading the images
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGRect xFirstFrame = m_pBackImageView.frame;
int nWidth = xFirstFrame.size.width;
int nHeight = xFirstFrame.size.height;
CGFloat rScaleX;
CGPoint xOrgCenter;
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
xOrgCenter = CGPointMake(384, 512);
}
else
{
if ([self appDelegate].isiPhone5 == YES)
{
xOrgCenter = CGPointMake(160, 284);
}
else
{
xOrgCenter = CGPointMake(160, 240);
}
}
CGContextRef buf_context = CGBitmapContextCreate(nil, nWidth, nHeight, 8, nWidth * 4, colorSpace, kCGImageAlphaPremultipliedLast);
CGColorSpaceRelease(colorSpace);
CGRect rect = CGRectMake(0,0, nWidth, nHeight);
CGRect xSecRect = m_pUserImgView.bounds;
UIGraphicsPushContext(buf_context);
CGContextClearRect(buf_context, rect);
CGContextDrawImage(buf_context, rect, m_pBackImageView.image.CGImage);
//Transform Context
CGAffineTransform pTransform = m_pUserImgView.transform;
CGAffineTransform xRealTrans = CGAffineTransformMake(pTransform.a, pTransform.b, pTransform.c, pTransform.d, pTransform.tx, -pTransform.ty);
CGPoint xOrgPoint = CGPointMake(xOrgCenter.x + pTransform.tx, xOrgCenter.y - pTransform.ty);
CGFloat rDegree = [self CalcRotateAngle:xRealTrans];
CGContextTranslateCTM(buf_context, xOrgPoint.x, xOrgPoint.y);
CGContextRotateCTM(buf_context, -rDegree);
rScaleX = sqrtf(pTransform.a * pTransform.a + pTransform.c * pTransform.c);
CGContextDrawImage(buf_context, CGRectMake(- xSecRect.size.width * rScaleX/2, - xSecRect.size.height * rScaleX/2, xSecRect.size.width * rScaleX , xSecRect.size.height * rScaleX), m_pUserImgView.m_pViewImage.CGImage);
UIGraphicsPopContext();
CGImageRef image = CGBitmapContextCreateImage(buf_context);
CGContextRelease(buf_context);
UIImage* pCombImage = [[UIImage alloc] initWithCGImage:image];
CGImageRelease(image);
return pCombImage;
Upvotes: 4
Views: 5725
Reputation: 785
You should create the context within drawRect
: method of a UIImageView subclass, then pass that context to your method.
Since your returning UIImage instance and creating ContextRef
within it, its clear that your doing your drawing without the use of the drawRect
:
What you can do 1. Create subclass of UIView or UIImageView, 2. Get the context within drawRect: 3. pass the context to the drawing method, by invoking the drawing method within drawRect: Like this
- (void)drawRect:(CGRect)rect
{
CGContextRef buf_context = UIGraphicsGetCurrentContext();
// CGContextRef buf_context = CGBitmapContextCreate(nil, nWidth, nHeight, 8, nWidth * 4, colorSpace, kCGImageAlphaPremultipliedLast);
UIImage *pCombImage = [self drawMyBeutifulImagesInRect:rect inContext: buf_context];
}
buf_context
... setNeedsDisplay
to force reDraw or refresh the drawing
. I hope this helps Upvotes: 0
Reputation: 2712
Try this: In xcode add symbolic breakpoint to CGPostError. (Add symbolic breakpoint, and to Symbol field type CGPostError)
When error happen, debugger will stop code executions and you can check stack of methods calls and check parameters. Look for 0 sized surfaces. Some param will have size of 0.
Upvotes: 10