2

I've used this snippet of code for years in my apps without fail. It cuts out a set of pieces from a CGImageRef I pass in and a black and white mask.

I am attempting to migrate the code into an enviornment where the layout will be based on autolayout and constraints.

It was worked perfectly with autolayout until I tried running it on an iPad 3, iPhone 5 or 4s.

After much research I believe that it is crashing due to memory alignment issues with this error.

EXC_BAD_ACCESS (code=EXC_ARM_DALIGN, address=0x5baa20)

I believe I need to adjust Bites/Bytes per component somehow to ensure the results always line up in memory to avoid angering the ARMV7 gods who rule over the problem devices.

I found some examples on the web but I have not successfully adapted them to this situation.

I have an alternate path where I can hard-code my sizing that doesn't cause the crash, but I have to abandon days worth of work on autolayout for hard coding.

Help please

    - (UIImage*) maskImage2:(CGImageRef)image withMask:(UIImage *)maskImage withFrame:(CGRect)currentFrame {
@autoreleasepool {

    CGFloat scale = [self adjustScale];


    CGRect scaledRect = CGRectMake(currentFrame.origin.x*scale, currentFrame.origin.y*scale, maskImage.size.width*scale, maskImage.size.height*scale);
    //Rect scaled up to account for iPad 3 sizing

    NSLog( @"%@ origin", NSStringFromCGRect(scaledRect));
    CGImageRef tempImage = CGImageCreateWithImageInRect(image, scaledRect);// Cut out the image at the size of the pieces
    CGImageRef maskRef = maskImage.CGImage;  //Creates the mask to cut out the fine edge and add backing layer

    CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),
                                        CGImageGetHeight(maskRef),
                                        CGImageGetBitsPerComponent(maskRef),
                                        CGImageGetBitsPerPixel(maskRef),
                                        CGImageGetBytesPerRow(maskRef),
                                        CGImageGetDataProvider(maskRef), NULL, false);
    CGImageRef colorsHighlights = CGImageCreateWithMask(tempImage, mask);//Colors with tran transparent surround

    CFRelease(tempImage);

    CGSize tempsize = CGSizeMake(maskImage.size.width, maskImage.size.height);

    CFRelease(mask);
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(tempsize.width+5, tempsize.height+5), NO, scale);
    CGContextRef context = UIGraphicsGetCurrentContext();

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        CGContextSetShadow(context, CGSizeMake(1, 2), 2);
    }else{
        CGContextSetShadow(context, CGSizeMake(.5, .5), 1);
    }
    [[UIImage imageWithCGImage:colorsHighlights scale:scale orientation:UIImageOrientationUp] drawInRect:CGRectMake(0, 0, tempsize.width, tempsize.height)]; // <<<< Crash is here
    CFRelease(colorsHighlights);

    UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return finalImage;
}

}

JeremyP
  • 84,577
  • 15
  • 123
  • 161
EJB
  • 21
  • 1
  • On which line does the crash occur? – JeremyP Sep 23 '14 at 16:31
  • It dies on [[UIImage imageWithCGImage:colorsHighlights scale:scale orientation:UIImageOrientationUp] drawInRect:CGRectMake(0, 0, tempsize.width, tempsize.height)]; – EJB Sep 23 '14 at 16:44

0 Answers0