ios - 重写值时 for 循环出错

标签 ios objective-c image-processing

我使用以下方法将输入图像转换为灰度和阈值:

UIImage *image = self.imageView.image;

    // Create image rectangle with current image width/height

    CGRect imageRect = CGRectMake(0, 0, image.size.width, image.size.height);
    NSLog(@"width %f, height %f", image.size.width, image.size.height  );

    // Grayscale color space
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();

    // Create bitmap content with current image size and grayscale colorspace
    CGContextRef context = CGBitmapContextCreate(nil, image.size.width, image.size.height, 8, 0, colorSpace, kCGImageAlphaNone);

    // Draw image into current context, with specified rectangle
    // using previously defined context (with grayscale colorspace)
    CGContextDrawImage(context, imageRect, [image CGImage]);

    // Create bitmap image info from pixel data in current context
    CGImageRef imageRef = CGBitmapContextCreateImage(context);

    // Create a new UIImage object
    UIImage *newImage = [UIImage imageWithCGImage:imageRef];

    // Release colorspace, context and bitmap information
    CGColorSpaceRelease(colorSpace);
    CGContextRelease(context);
    CFRelease(imageRef);

    // Return the new grayscale image
    self.imageView.image= newImage;




    //Thresholds the grayscale image



    CGImageRef sourceImage = newImage.CGImage ; //creates a CGImage reference for it

    //
    CFDataRef theData; //creates a variable of CFDataRef to store data of the image.
    //
    //
    //
    theData = CGDataProviderCopyData(CGImageGetDataProvider(sourceImage)); //assigns theData variable to the actual image
    //
    //
    //
    //
    //
    //
    //
    //
    UInt8 *pixelData = (UInt8 *) CFDataGetBytePtr(theData);
    //

    int dataLength = CFDataGetLength(theData);
    int counter=0;


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

        if (pixelData[index]  < 180)
        {
               NSLog(@"The intensity is %u", pixelData[index]);
            pixelData[index] = 0;
            //pixelData[index+1] = 0;
            //pixelData[index+2] = 0;
            //pixelData[index+3] = 0;

        }

        else
        {
            NSLog(@"The intensity is %u", pixelData[index]);
            pixelData[index] = 255;
           // pixelData[index+1] = 255;
           // pixelData[index+2] = 0;
            //pixelData[index+3] = 0;
        }





    }

当 for 循环尝试重写此处的强度时,应用程序崩溃:

pixelData[index] = 0;

有人可以帮我吗?

谢谢!

最佳答案

CFDataGetBytePtr 返回只读指针,如 Apple docs说。 提出解决方案here .

关于ios - 重写值时 for 循环出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17145374/

相关文章:

iphone - 一起使用 UINavigationController 和标签栏

image-processing - 自适应阈值二值化 : post-processing for removing ghost objects

android - 将位图(RGB_565)转换为灰度(8 位)Android

ios - Sprite Kit 纹理尺寸/分辨率

ios - 不同设备间的约束不一致

objective-c - 在运行时对 ObjectiveC 字符串进行字符串化

iOS9 supportedInterfaceOrientationsForWindow 停止调用

ios - 谷歌地图 markerInfowindow 委托(delegate)不调用 ios

ios - 如何在我的 UICollectionView 单元格中设置标签?

opencv - OpenCV的滚球背景减除算法