다음은이 사이트에서 복사 한 코드이며 원래 코드가 컴파일되지 않았으므로 약간만 수정 한 코드입니다. 가장자리 감지 및 색상 변경을위한 바이트 배열을 조작하고 싶지만, 먼저 기본 코드가 작동하도록하고 싶습니다. 현재 시스템이 컴파일되어 실행됩니다. 심하게 그려진 코끼리가 화면에 표시됩니다. 이미지를 터치하면 사라집니다. 스텝 진행은 imageWithData의 결과를 0x0으로 표시합니다. 나는 png와 bmp와 같은 결과로 이것을 시도했다.NSData에서 UiImage 만들기
내가 뭘 잘못하고 있는지에 대한 단서가있다!
@interface ImageViewDrawable : UIImageView
// I am using the following code to initialize this ImageView
ImageViewDrawable * uiv = [[ImageViewDrawable alloc] initWithImage:[UIImage imageNamed:@"ele.png"] ];
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
// get and work on pixel data
NSData* pixelData = (NSData*) CGDataProviderCopyData(CGImageGetDataProvider(self.image.CGImage));
char* bytes =[pixelData bytes];
// Take away the red pixel, assuming 32-bit RGBA
for(int i = 0; i < [pixelData length]; i += 4) {
bytes[i] = bytes[i]; // red
bytes[i+1] = bytes[i+1]; // green
bytes[i+2] = bytes[i+2]; // blue
bytes[i+3] = bytes[i+3]; // alpha
}
// convert pixel back to uiiimage
NSData* newPixelData = [NSData dataWithBytes:bytes length:[pixelData length]];
char * bytes2 =[pixelData bytes];
UIImage * newImage = [UIImage imageWithData:newPixelData] ;
[self setImage: newImage ];
}
그래서 내가 먹는 데이터는 본질적으로 기존 UIImage에서 나온 것이 중요하지 않습니까? –
내 문제를 참조하십시오. CGDataProviderCopyData (CGImageGetDataProvider()) - 데이터 공급자를 가져온 다음 JUST가 데이터를 복사합니다. 즉, 원래 이미지에서 픽셀을 가져오고 나머지 구성은 가져 오지 않습니다. 그래서 당신이 언급 한 픽셀과 CG 정보를 사용하여 이미지를 다시 만들어야합니다. 다시 도움 주셔서 감사합니다. –