由于项目需要,接手了一个IOS项目,主要工作内容是修改BUG,添加新功能。
之前一直都是从事windows c++的研发工作,没有接触过ios系统(包括xcode),一时间竟然不知所措。好在经过半个月的苦读,现在终于可以基本上手维护代码了。
以上算是背景。在一个新的功能修改中,被要求在UIImage中写入一组数字,这个在VC++中简单的不能再简单的功能,在ios中却让我绞尽脑汁,最后无赖我只能借助于搜索引擎的帮助。
从搜索引擎中,我开始是采用了如下的代码段来写入数字:
-(UIImage*)writeImageFooter:(NSString*)imagePath page:(int)pageIndex {
UIImage *img = [UIImage imageWithContentsOfFile:imagePath];
int w = img.size.width;
int h = img.size.height;
// rectangle's rect location
int posX = w - 50, posY = 7;
int rectW = 35, rectH = 22;
// font's position
int fontX = 0, fontY = 0;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);
CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1);
// m_pptTotalPage
NSString *pText = [NSString stringWithFormat:@"%d", (pageIndex + 1)];
char *text = (char*)[pText cStringUsingEncoding:NSASCIIStringEncoding];
CGContextSelectFont(context, "Arial", 18, kCGEncodingMacRoman);
CGContextSetTextDrawingMode(context, kCGTextFill);
// rotate text
//CGContextSetTextMatrix(context, CGAffineTransformMakeRotation(-M_PI / 4));
// fill rectangle as backgound
CGContextSetRGBFillColor(context, 255, 255, 255, 1);
CGContextFillRect(context, CGRectMake(posX, posY, rectW, rectH));
// calc font size
CGSize fontSize = [pText sizeWithFont:[UIFont systemFontOfSize:18.0]];
fontX = posX + ((rectW - fontSize.width) / 2);
fontY = posY + ((rectH - fontSize.height) / 2) + 3;
// write index to image
CGContextSetRGBFillColor(context, 0, 0, 0, 1);
CGContextShowTextAtPoint(context, fontX, fontY, text, strlen(text));
CGImageRef imageMasked = CGBitmapContextCreateImage(context);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
return[UIImage imageWithCGImage:imageMasked];
}
但是,不知为何,在写入百来张图片后,我的APP总是闪退,检查了一下是内存的原因导致的闪退,但是从这段代码中,我又没有检查出存在内存问题的地方。在经过两三天的较量后,我最终败下阵来,是在无能为力,我被迫寻求了另一种方法:
-(UIImage*)writeImageFooter:(int)pageIndex {
NSString *filePath = [g_pViewController.m_arrPPTImgs objectAtIndex:pageIndex];
if (filePath == nil)
{
return nil;
}
UIImage *img = [UIImage imageWithContentsOfFile:filePath];
if (img == nil)
{
return nil;
}
int w = img.size.width;
int h = img.size.height;
// rectangle's rect location
int posX = w - 50, posY = 7;
int rectW = 35, rectH = 22;
UIGraphicsBeginImageContext(img.size);
// fill image to new context
[img drawInRect:CGRectMake(0, 0, w, h)];
// get current new context
CGContextRef context = UIGraphicsGetCurrentContext();
if (context == nil)
{
return nil;
}
// adjust the coordinate system
//CGAffineTransform oldState = CGContextGetCTM(context);
CGContextTranslateCTM(context, 0, h);
CGContextScaleCTM(context, 1.0, -1.0);
// fill rectangle as backgound -- white
CGContextSetRGBFillColor(context, 255, 255, 255, 1);
CGContextFillRect(context, CGRectMake(posX, posY, rectW, rectH));
// calc font size and position
CGContextSelectFont(context, "Arial", 18, kCGEncodingMacRoman);
CGContextSetTextDrawingMode(context, kCGTextFill);
NSString *pText = [NSString stringWithFormat:@"%d", (pageIndex + 1)];
char *text = (char*)[pText cStringUsingEncoding:NSASCIIStringEncoding];
UIFont *font = [UIFont systemFontOfSize:18.0];
CGSize fontSize = [pText sizeWithFont:font];
int fontX = posX + ((rectW - fontSize.width) / 2),
fontY = posY + ((rectH - fontSize.height) / 2) + 3;
// write footer to image -- font color is black
CGContextSetRGBFillColor(context, 0, 0, 0, 1);
CGContextShowTextAtPoint(context, fontX, fontY, text, strlen(text));
// get new image
UIImage *pRetImg = UIGraphicsGetImageFromCurrentImageContext();
//CGContextConcatCTM(context, oldState);
UIGraphicsEndImageContext();
return pRetImg;
}
最终,这种方法完美的解决了我的问题,我的APP没有在闪退,APP在经过几百次的写入后,仍然健壮的运行着。
以上算是一点ios的编程心得,留着备用,或者留给遇到同样问题的coder们。