花费的时间和精力比我想象的要多,但万一有人在寻找相同的解决方案……显然在 iOS7 下无法工作,当我找到答案时会更新
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIImage *backgroundImage = [UIImage imageNamed:@"abstract_art_masterpiece_b.jpg"];
UIImage *textWithStrokeImage = [RJViewController drawTextWithStroke:@"unit 111"];
UIImage *image = [RJViewController placeImage:textWithStrokeImage onImage:backgroundImage];
self.imageView.image = image;
}
+ (UIImage*)placeImage:(UIImage*)image1 onImage:(UIImage*)image2 {
CGSize size = image2.size;
if ([UIScreen instancesRespondToSelector:@selector(scale)] && [[UIScreen mainScreen] scale] == 2.0f) {
UIGraphicsBeginImageContextWithOptions(size, NO, 2.0f);
} else {
UIGraphicsBeginImageContext(size);
}
[image2 drawAtPoint:CGPointMake(0, 0)];
[image1 drawAtPoint:CGPointMake(0, 0)];
UIImage* result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return result;
}
+ (UIImage*)drawTextWithStroke:(NSString*)string {
// set rect, size, font
CGRect rect = CGRectMake(0, 0, 88, 24);
CGSize size = CGSizeMake(rect.size.width, rect.size.height);
UIFont *font = [UIFont fontWithName:@"Helvetica-Bold" size:12];
// retina display, double resolution
if ([UIScreen instancesRespondToSelector:@selector(scale)] && [[UIScreen mainScreen] scale] == 2.0f) {
UIGraphicsBeginImageContextWithOptions(size, NO, 2.0f);
} else {
UIGraphicsBeginImageContext(size);
}
CGContextRef context = UIGraphicsGetCurrentContext();
// draw stroke
CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);
CGContextSetLineWidth(context, 4.0);
CGContextSetTextDrawingMode(context, kCGTextStroke);
[string drawInRect:CGRectIntegral(rect) withFont:font];
// draw fill
CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor);
CGContextSetTextDrawingMode(context, kCGTextFill);
[string drawInRect:CGRectIntegral(rect) withFont:font];
// convert to image and return
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}