重写值时for循环中的错误 [英] error in for loop when rewriting values

查看:137
本文介绍了重写值时for循环中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I used the following method to convert the input image to grayscale and threshold:

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循环试图重写强度时,应用程序崩溃:

The app crashes when the for loop is trying to rewrite the intensities here:

pixelData[index] = 0;

请有人帮帮我吗?

谢谢!

推荐答案

CFDataGetBytePtr 返回只读指针,如 Apple docs 说。
提出了一个解决方案这里

CFDataGetBytePtr returns read-only pointer, as Apple docs say. A solution is proposed here.

这篇关于重写值时for循环中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆