Ios 无法正确读取从UIView创建的UIImage的颜色数据
应该发生什么: 例如,用户使用苹果铅笔将图形绘制到UIView中。完成后,从该视图创建一个UIImage,一个接一个地读取ARGB数据,最后存储到NSData中 稍后,NSData会再次用于重新创建图像。我知道我可以更容易地实现类似的目标,但请假设有一个很好的理由来实现这个目标 实际发生的情况: 当用户进入视图时,会拍摄UIImage,但是当代码访问图像数据时,会发生一些事情:数据似乎不适合图像,这在使用它们重建图像时变得很明显 这是代码 从UIViewIos 无法正确读取从UIView创建的UIImage的颜色数据,ios,objective-c,uiview,bitmap,uiimage,Ios,Objective C,Uiview,Bitmap,Uiimage,应该发生什么: 例如,用户使用苹果铅笔将图形绘制到UIView中。完成后,从该视图创建一个UIImage,一个接一个地读取ARGB数据,最后存储到NSData中 稍后,NSData会再次用于重新创建图像。我知道我可以更容易地实现类似的目标,但请假设有一个很好的理由来实现这个目标 实际发生的情况: 当用户进入视图时,会拍摄UIImage,但是当代码访问图像数据时,会发生一些事情:数据似乎不适合图像,这在使用它们重建图像时变得很明显 这是代码 从UIView -(UIImage *)imageRep
-(UIImage *)imageRepresentation{
UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 1.0);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
myImage= UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return myImage;
}
初始化
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.layer.allowsEdgeAntialiasing=YES;
self.backgroundColor=[UIColor clearColor];
}
return self;
}
绘图
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches]anyObject];
CGPoint touchLocation = [touch locationInView:self];
if (myImage!=nil) {
UIImageView * iV = [[UIImageView alloc] initWithFrame:self.bounds];
iV.image = myImage;
[self insertSubview:iV atIndex:0];
}
myPath = [UIBezierPath bezierPath];
[myPath moveToPoint:touchLocation];
}
- (void) touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches]anyObject];
CGPoint touchLocation = [touch locationInView:self];
[myPath addLineToPoint:touchLocation];
[self setNeedsDisplay];
}
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
myImage = self.imageRepresentation;
}
- (void) drawRect:(CGRect)rect {
self.context = UIGraphicsGetCurrentContext();
[[UIColor blackColor]setStroke];
myPath.lineWidth=3.0;
[myPath stroke];
}
读取和转换图像数据
-(NSData *) pixel{
int sidelength = (int) self.myImage.size.width;
int numberData = sidelength * sidelength * 4;
CFDataRef pixelData = CGDataProviderCopyData(CGImageGetDataProvider(self.myImage.CGImage));
const UInt8 * data = CFDataGetBytePtr(pixelData);
UInt8 pixel[numberData];
for (int y=0; y<sidelength; y++) {
for (int x=0; x<sidelength; x++) {
int components = sidelength * y + x * 4;
UInt8 a = data[components];
UInt8 r = data[components+1];
UInt8 g = data[components+2];
UInt8 b = data[components+3];
pixel[components]=a;
pixel[components+1]=r;
pixel[components+2]=g;
pixel[components+3]=b;
}
}
CFRelease(pixelData);
return [NSData dataWithBytes:pixel length:sidelength*sidelength*4];;
下面是一个结果示例:
使用imageRepresentation渲染的原始图形:
使用pixel和getImage检索的图片:
我认为罪魁祸首是“像素”方法,因为我检查了它的结果,发现它们已经非常错误了。另外,我在不同的环境中测试了“getImage”,它工作得很好。但奇怪的是,“像素”也是如此。那么,UIView是否有可能以不同于png或jpg的方式呈现?我查看了UIGraphicsBeginImageContextWithOptions的文档,但没有提供有用的线索。有两个问题。第一个问题是,在像素方法中,没有将所有图像数据插入像素阵列 如果图像为100x100,则字节总数为10000乘以4。在for循环中,获得的最大像素为100x100+100x4,即10400。其余像素包含垃圾 此循环将完成以下工作:
for (unsigned long i = 0; i < numberData; i += 4) {
pixel[i] = data[i];
pixel[i + 1] = data[i + 1];
pixel[i + 2] = data[i + 2];
pixel[i + 3] = data[i + 3];
}
第二个问题更有趣。如果重新创建的图像的宽度不是64的乘积,则该图像将被扭曲。CGBitmapContextCreate中存在某种缺陷——大约六年前,我将该缺陷提交给了苹果公司,他们声称已经修复了该缺陷,但在某些条件下可以很容易地复制该缺陷
我使用新循环运行了您的代码,它在128x128大小的UIView上运行良好,但在大小为100x100时返回扭曲图像。您假设您的图像是正方形,其高度等于宽度。你确定这个假设是正确的吗?另一件事:谢谢。是的,我将视图初始化为LetterView,它是UIView的一个子类,来自这样的父视图:self.LetterView=[[LetterView alloc]initWithFrame:CGRectMake0,0,frame.size.width,frame.size.width];哇,第一部分差点让人难堪。我想我得上计数课了。非常感谢!顺便说一句,我在meatime中添加了一些代码,将图像转换为png,并且似乎没有遇到第二个问题。但无论如何,如果我这样做了,我现在知道该怎么做了。
for (unsigned long i = 0; i < numberData; i += 4) {
pixel[i] = data[i];
pixel[i + 1] = data[i + 1];
pixel[i + 2] = data[i + 2];
pixel[i + 3] = data[i + 3];
}