9

I wan't a imageview which is semi-transparent in other parts and completely transparent in the middle .. see this image.. enter image description here

i have managed to do it but it is not the best of ways.. can I only have a rect inside yellow lines transparent ...?

EDIT: Is it possible to change the alpha of a certain rect of a view..?

Ankit Srivastava
  • 12,347
  • 11
  • 63
  • 115
  • @Leena it sure is posible.. see the default camera application on iPhone and edit the image once it is clicked... you will see the very same feature... – Ankit Srivastava Aug 03 '12 at 09:21
  • i had created similar kind of functionality like this when i had implemented crop functionality in my app but not using single view. – Leena Aug 03 '12 at 09:26
  • so can you confirm that certain rect can have different alpha?? According to me, it must be doing some other thing but not exactly alpha. – Tarun Aug 03 '12 at 09:38
  • i had specified that with single view its not possible i had made use of two imageviews and one view with low alpha – Leena Aug 03 '12 at 09:45
  • Is this rectangle resizable / draggable like the crop feature of the ImagePicker? – Eric Genet Jan 28 '13 at 11:54
  • no.. this was supposed to be fix in my requirement. – Ankit Srivastava Jan 28 '13 at 12:26

6 Answers6

24

Place a custom UIView over the view showing the image, size and position it to exactly overlap. In the custom view's drawRect method

- (void)drawRect:(CGRect)rect
{
    // Drawing code
    CGRect rBounds = self.bounds;

    CGContextRef context = UIGraphicsGetCurrentContext();

    // Fill background with 80% white
    CGContextSetFillColorWithColor(context, [[[UIColor whiteColor] colorWithAlphaComponent:0.8] CGColor]);
    CGContextFillRect(context, rBounds);

    // Draw the window 'frame'
    CGContextSetStrokeColorWithColor(context, [[UIColor orangeColor] CGColor]);
    CGContextSetLineWidth(context, 10);
    CGContextStrokeRect(context, self.maskRect);

    // make the window transparent
    CGContextSetBlendMode(context, kCGBlendModeClear);
    CGContextFillRect(context, self.maskRect);
}

where maskRect is the rectangle to be shown transparent. Do ensure that the background color of the custom view is set to 'clearColor'

You can write any additional logic required to change the maskRect and call 'setNeedsDisplay' on this view.

P.J.Radadiya
  • 1,493
  • 1
  • 12
  • 21
Gopal R
  • 732
  • 5
  • 8
2

After lots of effort, I have found the solution.

Here is the code for it!!

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

    [screenShotButton setHidden:YES];
    UITouch *touch = [touches anyObject];
    CGPoint pnt =[touch locationInView:self.view];
    CGRect viewFrame=changeView.frame;
    CGFloat x1Diff,y1Diff,x2Diff,y2Diff;
    x1Diff=ABS(viewFrame.origin.x-pnt.x);
    y1Diff=ABS(viewFrame.origin.y-pnt.y);
    x2Diff=ABS(viewFrame.origin.x+viewFrame.size.width-pnt.x);
    y2Diff=ABS(viewFrame.origin.y+viewFrame.size.height-pnt.y);
    if (x1Diff<=10) {
        left=YES;
        shouldExpand=YES;
    }
    if (y1Diff<10) {
        top=YES;
        shouldExpand=YES;
    }
    if (x2Diff<=10) {
        right=YES;
        shouldExpand=YES;
    }
    if (y2Diff<=10) {
        bottom=YES;
        shouldExpand=YES;
    }

    if(CGRectContainsPoint(changeView.frame, pnt))
    {
        shouldChange=YES;
        fromPoint=pnt;
    }
    else
    {
        shouldChange=NO;
    }


}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{

    if (!shouldChange) {
        return;
    }

    if (shouldExpand)
    {
        UITouch *touch = [touches anyObject];
        CGPoint pnt =[touch locationInView:self.view];
        CGRect preFram =changeView.frame;
        CGRect modifiedFrame=preFram;
        if (left) {
            modifiedFrame.origin.x=preFram.origin.x-(fromPoint.x-pnt.x);            
            modifiedFrame.size.width=preFram.size.width+fromPoint.x-pnt.x;
            fromPoint.x=pnt.x;
        }
        if (right) {
            modifiedFrame.size.width=preFram.size.width+pnt.x-fromPoint.x;
            fromPoint.x=pnt.x;

        }
        if (top) {
            modifiedFrame.origin.y=preFram.origin.y-(fromPoint.y-pnt.y);
            modifiedFrame.size.height=preFram.size.height+fromPoint.y-pnt.y;
            fromPoint.y=pnt.y;
        }
        if (bottom) {
            modifiedFrame.size.height=preFram.size.height+pnt.y-fromPoint.y;
            fromPoint.y=pnt.y;
        }
        changeView.frame=modifiedFrame;
        [clearImage setBounds:CGRectMake(0-modifiedFrame.origin.x, 0-modifiedFrame.origin.y, clearImage.frame.size.width, clearImage.frame.size.height)];

    }
    else
    {
        UITouch *touch = [touches anyObject];
        CGPoint pnt =[touch locationInView:self.view];
        changeView.center=CGPointMake(changeView.center.x-fromPoint.x+pnt.x, changeView.center.y-fromPoint.y+pnt.y);
        [clearImage setBounds:CGRectMake(0-changeView.frame.origin.x, 0-changeView.frame.origin.y, clearImage.frame.size.width, clearImage.frame.size.height)];
        fromPoint=pnt;
    }



}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [screenShotButton setHidden:NO];
    left=top=right=bottom=NO;
    shouldExpand=NO;
    fromPoint=CGPointZero;
    toPoint=CGPointZero;
    shouldChange=NO;
}

- (IBAction)takeScreenShot:(id)sender
{
    [screenShotButton setHidden:YES];
    UIGraphicsBeginImageContext(self.view.frame.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextScaleCTM(context, 1.0, 1.0);
    [self.view.layer renderInContext:context];
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *filePath = [NSString stringWithFormat:@"%@/1.png",docPath];
    CGRect cutRect = changeView.frame;
    CGImageRef imgRef = CGImageCreateWithImageInRect([img CGImage], cutRect);
    CFURLRef url = (__bridge CFURLRef)[NSURL fileURLWithPath:filePath];
    CGImageDestinationRef destination = CGImageDestinationCreateWithURL(url, kUTTypePNG, 1, NULL);
    CGImageDestinationAddImage(destination, imgRef, nil);
    CFRelease(imgRef);

    if (!CGImageDestinationFinalize(destination)) {
        NSLog(@"Failed to write image to %@", filePath);
    }
    img=nil;
    imgRef=nil;
    CFRelease(destination);
    [screenShotButton setHidden:NO];
    [changeView setHidden:NO];
}

Just, logic mattered here!!

Bharath
  • 3,001
  • 6
  • 32
  • 65
1

Why dont you do it like this only:

  1. Create a imageview set its alpha to make it semitransparent
  2. Then just crop the part of the image that you want to be fully transparent and create a new imageview with alpha = 1 and set that cropped image in it only above the semi-transparent imageview :)
Bhupendra
  • 2,525
  • 18
  • 20
  • 1
    I want the fully transparent view to be extended i.e., similar to default camera application's edit image thing – Bharath Jan 29 '13 at 08:26
0

ss

I think you maybe be able to change the outside of selected rectangle. As for below image, you should 4 alphas of 4 rectangles. For example, at first you set white windows background color, then the 4 alpas of rectangles set to 0.5.

I think you would'd better use CALayer instead of UIView.

I wish my advice help your alittle.

Community
  • 1
  • 1
Feel Physics
  • 2,783
  • 4
  • 25
  • 38
0

I think u can use CGContext

void CGContextDrawRadialGradient(
  CGContextRef context,
  CGGradientRef gradient,
  CGPoint startCenter,
  CGFloat startRadius,
  CGPoint endCenter,
  CGFloat endRadius,
  CGGradientDrawingOptions options
);

see link below CGContext reference or CGGradient reference

Sathe_Nagaraja
  • 163
  • 1
  • 9
  • Could you please explain in brief on how to achieve this? Just giving the reference document is not enough. I am still not getting on how to use the above reference to achieve the above feature. – Bharath Jan 28 '13 at 10:32
0

A simple method

   1:Create a image in photoshop with the alpha values which you have specified.
   2:Place the new imageview  over your image .
   3:If you want to move the transparent box ,change the image frame.
   4:If you want to expand you just change the size of upper image.
Justin Mathews
  • 512
  • 4
  • 18
  • @steve.. Please read the question completely before you answer!! Here, its not about photoshop & all. We want to achieve this at run time. Its possible & I have dont it!! – Bharath Jan 29 '13 at 13:08