I was followed by this tutorial to draw text using Core Text, and I want to add web image to the view. when I used the code below, everything works fine except waiting the downloading time.
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetTextMatrix(context, CGAffineTransformIdentity);
CGContextTranslateCTM(context, 0, self.bounds.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CTFrameDraw((CTFrameRef)self.coreTextFrame, context);
for (NSArray *imageData in self.images) {
UIImage *image = [UIImage imageWithData:[[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[imageData objectAtIndex:0]]]];
CGRect imageBounds = CGRectFromString([imageData objectAtIndex:1]);
CGContextDrawImage(context, imageBounds, image.CGImage);
}
}
but this code blocked the main thread, and when I replace the code like this:
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetTextMatrix(context, CGAffineTransformIdentity);
CGContextTranslateCTM(context, 0, self.bounds.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CTFrameDraw((CTFrameRef)self.coreTextFrame, context);
for (NSArray *imageData in self.images) {
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString: [imageData objectAtIndex:0]]];
__block UIImage *image = nil;
CGRect imageBounds = CGRectFromString([imageData objectAtIndex:1]);
AFImageRequestOperation *operation = [AFImageRequestOperation imageRequestOperationWithRequest:request success:^(UIImage *downloadImage){
image = downloadImage;
CGContextDrawImage(context, imageBounds, image.CGImage);
}];
[operation start];
}
}
it becomes wired, the position of image is wrong.