我正在后台线程中获取图像并将其保存到临时目录。在保存之前,我想给这个 UIImage 添加一点白色边框。我这样做:
- (UIImage *)imageWithBorder:(UIImage *)image {
CGSize newSize = CGSizeMake(image.size.width, image.size.height);
CGRect rect = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
UIGraphicsBeginImageContextWithOptions(newSize, NO, image.scale); {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextBeginTransparencyLayer (context, NULL);
//Draw
[image drawInRect:rect blendMode:kCGBlendModeNormal alpha:1.0];
CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);
CGContextStrokeRectWithWidth(context, rect, 10);
CGContextEndTransparencyLayer(context);
}
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return result;
}
一切正常,从临时目录加载该图像后,我可以看到图像上的边框,但在控制台中,当下面的代码实现时,我得到了 invalid context 0x0
。这里有什么问题吗?
更新 01
- (UIImage *)imageWithBorder:(UIImage *)image {
CGSize newSize = CGSizeMake(image.size.width, image.size.height);
CGRect rect = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
UIGraphicsBeginImageContextWithOptions(newSize, NO, image.scale); {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextDrawImage (context, rect, [image CGImage]);
CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);
CGContextStrokeRectWithWidth(context, rect, 10);
}
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return result;
}
最佳答案
尝试改变:
[image drawInRect:rect blendMode:kCGBlendModeNormal alpha:1.0];
到
CGContextDrawImage (context, rect, [image CGImage]);
另外,为什么要使用透明层?您将 alpha 设置为 1,所以您可以覆盖原始图像,不是吗?
编辑:我只是在我的应用程序中运行了相同的代码,在后台,它运行完美:
- (UIImage *)imageWithBorder:(UIImage *)image
{
NSLog(@"Thread %@", [NSThread currentThread]);
CGSize newSize = CGSizeMake(image.size.width, image.size.height);
CGRect rect = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
UIGraphicsBeginImageContextWithOptions(newSize, NO, image.scale);
CGContextRef context = UIGraphicsGetCurrentContext();
//Draw
CGContextDrawImage(context, rect, [image CGImage]);
CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);
CGContextStrokeRectWithWidth(context, rect, 10);
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
assert(result);
return result;
}
当我在 viewDidAppear 中执行此操作时:(但它是一个小图像,也许大小是相关的?)
dispatch_async(dispatch_get_global_queue(0, 0), ^{ [self imageWithBorder:[UIImage imageNamed:@"02-redo.png"]] ; } );
这是我在日志中看到的全部内容:
2012-08-27 13:48:04.679 Searcher[32825:11103] Thread <NSThread: 0x6d30e30>{name = (null), num = 4}
所以您的代码发生了其他事情。我正在使用 Xcode 4.4.1 构建,部署目标是 5.1
编辑:在您的 block 中,您不能引用“单元格”,因为单元格是暂时的 - 回收它甚至可能不可见。因此图像名称将通过本地 NSString 进行路由,然后最后,您需要另一个可以将图像发布到的方法以及单元格 indexPath。该方法将查看该 indexPath 是否在表格的 visibleCells 中,如果是,则更新图像,如果不是,则您想要存储图像以便它可以与 tableView 一起使用,接下来请求单元格。
EDIT2:这段代码:
dispatch_async(queue, ^{
UIImage *image = [_thumbnailMaster thumbnailForItemWithFilename:cell.label.text];
dispatch_sync(dispatch_get_main_queue(), ^{ cell.thumbnailView.image = image; });
});
应该变成不涉及“cell”的东西,因为 cell 可能超出范围或被另一个索引使用(假设回收)。像这样:
NSString *text = cell.label.text;
NSIndexPath *path = ...;
dispatch_async(queue, ^{
UIImage *image = [_thumbnailMaster thumbnailForItemWithFilename:text];
dispatch_sync(dispatch_get_main_queue(), ^{ [self updateCellImage:image withPath"path]; });
});
您的方法确定该单元格是否可见,如果可见则更新图像,如果不可见则保存它以便下次您需要它时拥有它。
关于iphone - 带边框的 UIImage。无效上下文 0x0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12145977/