0

there. How can I chage CGImageRef from another ViewController?

I have this code:

#import "ScratchableView.h"

@implementation ScratchableView

- (id)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];
    if (self) {

      //init with the scratchable gray image




           scratchable = [UIImage imageNamed:@"image.jpg"].CGImage;

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

    self.opaque = NO;
    CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceGray();

    CFMutableDataRef pixels = CFDataCreateMutable( NULL , width * height );
    alphaPixels = CGBitmapContextCreate( CFDataGetMutableBytePtr( pixels ) , width , height , 8 , width , colorspace , kCGImageAlphaNone );
    provider = CGDataProviderCreateWithCFData(pixels);

    CGContextSetFillColorWithColor(alphaPixels, [UIColor blackColor].CGColor);
    CGContextFillRect(alphaPixels, frame);

    CGContextSetStrokeColorWithColor(alphaPixels, [UIColor whiteColor].CGColor);
    CGContextSetLineWidth(alphaPixels, 30.0);
    CGContextSetLineCap(alphaPixels, kCGLineCapSquare);

    CGImageRef mask = CGImageMaskCreate(width, height, 8, 8, width, provider, nil, NO);
    scratched = CGImageCreateWithMask(scratchable, mask);



    CGImageRelease(mask);
    CGColorSpaceRelease(colorspace);

    }
    return self;
}

Now From another view controller I want to change the image.jpg with image2.jpg. Please, help me, how can I do that?

I tried with app delegate, with nsstrings, but no succes. I'm beginner.

user1625435
  • 153
  • 8
  • How is `ScratchableView` declared? What does it inherit from? How are you displaying `image.jpg`? – user1118321 Aug 26 '12 at 05:35
  • - (void)awakeFromNib { //Adding a scratchable view with the scratchable image scratchableView = [[ScratchableView alloc] initWithFrame:self.frame]; [self addSubview:scratchableView]; backimg = [NSString stringWithFormat:@"backgr1.jpg"]; //adding another image under the scratchable image. UIImageView *background = [[UIImageView alloc] initWithImage:[UIImage imageNamed:backimg]]; [self addSubview:background]; [self bringSubviewToFront:scratchableView]; } – user1625435 Aug 26 '12 at 05:44
  • i have CGScratch project from github, and I want to swipe, or shake to change both images, can you help me please.. – user1625435 Aug 26 '12 at 05:51

1 Answers1

0

The two common ways to let one object influence another is to either provide public properties that can be set, or provide a method + parameter on your class. In both cases the object wanting to make the change needs some way to access the second object. Common ways to do that are to find it in the UINavigation viewControllers array, or though a tabBarController, through the appDelegate, or the second class is a singleton and can be found by asking the class object for the reference.

When you get this sorted out, the calling class with do something like send a message having this signature:

- (BOOL)updateImage:(CGImageRef)theNewImage
David H
  • 40,852
  • 12
  • 92
  • 138