English 中文(简体)
App Crashes when aiming at Update a UIImageView after changinging the RGB Value for anOS Project
原标题:App Crashes when trying to update a UIImageView after modifying the RGB values for an iOS project

我试图对图像施加多重影响。 I ve创建了一个单独的档案,处理我可以发送并接收经过修改的副本的效应处理。 为了节省处理时间,我首先把图像装入单独的处理档案,并把它储存在记忆中,而不是在我每次想要修改时装上图像。 流动量为:获得、开发、改造、开发、展示。 一切都持续到最后一步。 回归的图像在二分之一的屏幕上显示,然后在X射线上坠毁,有<条码>EXEC_ACCESS:代码1错误。 我一再走过这部法典,可以找到问题。 任何帮助都受到高度赞赏。 谢谢!

www.un.org/spanish/ecosoc 网址:

I m 采用Xcode 4.3.1, 自动参照法

Using Line Breaks, I can verify that the crash happens when the line self.imageView.image = [self.imageManipulation displayImage]; is executed. The image IS updated, but then the program immediately crashes. Using NSZombie, i get an error -[Not A Type retain]: message sent to deallocated instance 0x2cceaf80

从我的角度来看,主计长 我使用:

[self.imageManipulation getImageData:self.imageView.image];
[self.imageManipulation modifyRGB];
self.imageView.image = [self.imageManipulation displayImage];

我的图像管理文件包括:

@implementation ImageManipulation
static unsigned char *rgbaDataOld;
static unsigned char *rgbaDataNew;
static int width;
static int height;

- (void)getImageData:(UIImage *)image
{
    CGImageRef imageRef = [image CGImage];

    width = CGImageGetWidth(imageRef);
    height = CGImageGetHeight(imageRef);

    rgbaDataOld = malloc(height * width * 4);
    rgbaDataNew = malloc(height * width * 4);

    NSUInteger bytesPerPixel = 4;
    NSUInteger bytesPerRow = bytesPerPixel * width;
    NSUInteger bitsPerComponent = 8;

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    CGContextRef context = CGBitmapContextCreate(rgbaDataOld, width, height, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);

    CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);

    CGColorSpaceRelease(colorSpace);
    CGContextRelease(context);
    CGImageRelease(imageRef);
}

//modify rgb values 
- (void)modifyRGB
{
    for (int byteIndex = 0 ; byteIndex < width * height * 4; byteIndex += 4)
    {
        rgbaDataNew[byteIndex] = (char) (int) (rgbaDataOld[byteIndex] / 3) + 1;
        rgbaDataNew[byteIndex+1] = (char) (int) (rgbaDataOld[byteIndex+1] / 3 + 1);
        rgbaDataNew[byteIndex+2] = (char) (int) (rgbaDataOld[byteIndex+2] / 3) + 1;
        rgbaDataNew[byteIndex+3] = (char) (int) 255;
    }    
}

//set image
- (UIImage *)displayImage
{
    CGContextRef context;

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    context = CGBitmapContextCreate(rgbaDataNew, width, height, 8, 4 * width, colorSpace, kCGImageAlphaPremultipliedLast ); 

    CGImageRef imageRef = CGBitmapContextCreateImage (context);
    UIImage *outputImage = [UIImage imageWithCGImage:imageRef];  

    CGColorSpaceRelease(colorSpace);    
    CGContextRelease(context);  
    CGImageRelease(imageRef);
    return outputImage;
}

@end
最佳回答

我改变了我的dispayImage()方法,以获得UIImageView财产和返回VOID。 这样,所有工作都是通过<>UIImageView进行的,而不是局部性的,任何工作都不回过。

我的猜测是,在坠毁之前,我使用的返回UIImage办法,因为所交回的参考线被放在第二位完成的方法上。 这也使我得以在没有任何影响的情况下使用

这里采取的新做法:

- (void)displayImage:(UIImageView *)image
{
   CGContextRef context;
   CGImageRef imageRef = [image.image CGImage];

   CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
   context = CGBitmapContextCreate(rgbaDataNew, width, height, 8, 4 * width, colorSpace,  kCGImageAlphaPremultipliedLast ); 
   imageRef = CGBitmapContextCreateImage (context);
   image.image = [UIImage imageWithCGImage:imageRef];
   CGColorSpaceRelease(colorSpace);    
   CGContextRelease(context);  
   CGImageRelease(imageRef);
   image = nil;
}

Thank you to everyone that offered help! I learned a tremendous amount just from your suggestions. This was my first time using bt and NSZombie and now I m using them religiously! Thanks again!

问题回答

你们释放图像,使UIImage自动释放。

确保你保留“UIImage”,以防止发生此类事件:

- (UIImage *)displayImage
{
    CGContextRef context;

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    context = CGBitmapContextCreate(rgbaDataNew, width, height, 8, 4 * width, colorSpace, kCGImageAlphaPremultipliedLast ); 

    CGImageRef imageRef = CGBitmapContextCreateImage (context);
    UIImage *outputImage = [[UIImage imageWithCGImage:imageRef] retain]; 

    CGColorSpaceRelease(colorSpace);    
    CGContextRelease(context);  
    CGImageRelease(imageRef); //this line would cause outputImage to be released
    return outputImage;
}

在<代码>上<0>。 请通过<_strong将图像保留给编辑人:

UIImage __strong *outputImage = [UIImage imageWithCGImage:imageRef];

或者如果你选择:

__strong UIImage *outputImage = [UIImage imageWithCGImage:imageRef];




相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...