-1

I am using gyroscope to handle rotation. For every degree that I rotate iPad, I should redraw the image on the screen to change the height of the mask.

But redrawing stops the gyroscope.

What can I do for this situation?

Edit code added

- (UIImage *)reflectedImage:(UIImageView *)fromImage withHeight:(NSUInteger)height
{
CGImageRef gradientMaskImage = CreateGradientImage(1, height);

CGImageRef masked = CGImageCreateWithMask([fromImage.image CGImage], gradientMaskImage);
CGImageRelease(gradientMaskImage);

UIImage *theImage = [UIImage imageWithCGImage:masked];

return theImage;
}

Gyroscope will give me a value, and I calculate a height for the image. After that I call this function to redraw the mask and image. So if I roll the device, image will blind up or blind down.

Undo
  • 25,519
  • 37
  • 106
  • 129
Burak
  • 5,706
  • 20
  • 70
  • 110
  • Show he code in question so we have something to work with. – WrightsCS Dec 20 '11 at 04:35
  • Keep in mind the purpose of Apple sample apps which is often the simplest showcase. I found `CAReplicatorLayer` performance to be pretty good, try to use it if possible. – A-Live Jun 07 '13 at 22:03

1 Answers1

1

You can redraw the image in a background thread. When the image is ready, update the UI on the main thread.

static dispatch_queue_t background_queue;
- (void)updateImage {
  if (background_queue == nil){
        background_queue = dispatch_queue_create("com.myappname.myIdentifier", 0);
  }

  dispatch_async(background_queue, ^ {  // render the image in the background thread
     UIImage * theImage = [self reflectedImage:fromImage withHeight:height]; 
     dispatch_sync(dispatch_get_main_queue(), ^{
        imageView.image = theImage;    // Update the imageview in the main thread
     });
  });
}

Edit: code fixes

lorean
  • 2,150
  • 19
  • 25
  • Is there a realtime solution of that? Because I guess this will be asynchronous. – Burak Dec 20 '11 at 20:37
  • The images will be rendered and displayed in the correct order (i.e. frame(t) will render before frame(t+1) for all t. If you need a faster solution, then you'll have to find some way to optimize the graphics rendering. Or possibly generate and cache all possible images (360?) – lorean Dec 20 '11 at 20:49
  • Actually I only want to change the gradient layer height. So I shouldn't redraw all the image. But how can I do this? – Burak Dec 20 '11 at 20:55
  • Maybe you can try experimenting with different blend modes? You'll have to override the drawRect: method to do your custom drawing. – lorean Dec 21 '11 at 18:12