按位命令使访问错误

时间:2013-11-15 09:23:14

标签: ios objective-c

我正在编写一个应用程序来将图像信息隐藏到另一个图像中!我通过从第一个图像中删除4个低电平位并从第二个图像中添加4个高电平位来实现这一点! 这是我的代码:

    CGImageRef publicImage = _publicImageView.image.CGImage;
CFDataRef thePublicData;
thePublicData = CGDataProviderCopyData(CGImageGetDataProvider(publicImage));
UInt8 *publicPixelData = (UInt8 *) CFDataGetBytePtr(thePublicData);
int publicDataLength = CFDataGetLength(thePublicData);

//__________________________________________________________________________

CGImageRef secretImage = _secretImageView.image.CGImage;
CFDataRef theSecretData;
theSecretData = CGDataProviderCopyData(CGImageGetDataProvider(secretImage));
UInt8 *secretPixelData = (UInt8 *) CFDataGetBytePtr(theSecretData);
int secretDataLength = CFDataGetLength(theSecretData);


int red = 0;
int green = 1;
int blue = 2;

for (int index = 0 ; index < publicDataLength && index < secretDataLength; index +=4) {

    uint pRed = publicPixelData [index + red] % 0x10;
    publicPixelData [index + red] -= pRed;

    uint sRed = secretPixelData [index + red] / 0x10;
    publicPixelData[index + red] += sRed;

    uint pGreen = publicPixelData [index + green] % 0x10;
    publicPixelData [index + green] -= pGreen;

    uint sGreen = secretPixelData [index + green] / 0x10;
    publicPixelData[index + green] += sGreen;

    uint pBlue = publicPixelData [index + blue] % 0x10;
    publicPixelData [index + blue] -= pBlue;

    uint sBlue = secretPixelData [index + blue] / 0x10;
    publicPixelData[index + blue] += sBlue;

}

CGContextRef context;
context = CGBitmapContextCreate(publicPixelData,
                                CGImageGetWidth(publicImage),
                                CGImageGetHeight(publicImage),
                                8,
                                CGImageGetBytesPerRow(publicImage),
                                CGImageGetColorSpace(publicImage),
                                kCGImageAlphaPremultipliedLast);

CGImageRef newCGImage = CGBitmapContextCreateImage(context);
UIImage *newImage = [UIImage imageWithCGImage:newCGImage];

CGContextRelease(context);
CFRelease(thePublicData);
CGImageRelease(newCGImage);

_hideEffectImageView.image = newImage;

要恢复第二张图像,我将“__hideEffectImageView”数据的位移到高位!

以下是此代码:

    CGImageRef stganoImage = _hideEffectImageView.image.CGImage;
CFDataRef stganoData;
stganoData = CGDataProviderCopyData(CGImageGetDataProvider(stganoImage));
UInt8 *steganoPixelData = (UInt8 *) CFDataGetBytePtr(stganoData);
int publicDataLength = CFDataGetLength(stganoData);



   //__________________________________________________________________________
 //   CGImageRef secretImage;
 //   CFDataRef   theSecretData;
 //   UInt8 *secretPixelData = (UInt8 *)CFDataGetBytePtr(stganoData);

    int red = 0;
    int green = 1;
    int blue = 2;


    for (int index = 0 ; index < publicDataLength; index +=4) {

        steganoPixelData [index + red] = steganoPixelData [index + red] << 0x4 ;
        steganoPixelData [index + green] = steganoPixelData [index + red] << 0x4 ;       
        steganoPixelData [index + blue] = steganoPixelData [index + red] << 0x4 ;
    }

    CGContextRef context;
    context = CGBitmapContextCreate(steganoPixelData,
                                    CGImageGetWidth(stganoImage),
                                    CGImageGetHeight(stganoImage),
                                    8,
                                    CGImageGetBytesPerRow(stganoImage),
                                    CGImageGetColorSpace(stganoImage),
                                    kCGImageAlphaPremultipliedLast);

    CGImageRef newCGImage = CGBitmapContextCreateImage(context);
    UIImage *newImage = [UIImage imageWithCGImage:newCGImage];

    CGContextRelease(context);
    CFRelease(stganoData);
    CGImageRelease(newCGImage);

    _recoverSecretImage.image = newImage;

在某些图像中,我在代码“steganoPixelData [index + red] = steganoPixelData [index + red]&lt;&lt; 0x4;”的行中获得“EXC_BAD_ACCESS(Code = 2”和Address ...)

我需要知道为什么这些错误出现在某些图片中,我该如何修复它。

非常感谢...

1 个答案:

答案 0 :(得分:1)

这可能是零。有最可能的解释。请登录到您steganoPixelData的控制台。我很确定这可能是零,因为你的操作是允许的,你在这个等式中只有一个数据结构:steganoPixelData[index + red] <<= 0x4

也许你的形象是零?检查一下。可能已取消分配。