Pages

Showing posts with label UIImageView. Show all posts
Showing posts with label UIImageView. Show all posts

Wednesday, September 25, 2013

Align UIImageView Center dynamically in xCode

Suppose we have an UIImageView with name imageView and have to set this in center dynamically then we can follow the below step

[imageView setImage:[UIImage imageNamed:@"imageName.png"]];
imageView.autoresizingMask = UIViewAutoresizingNone;

imageView.contentMode = UIViewContentModeCenter;

Monday, September 23, 2013

CREATION OF IMAGE VIEW IN CIRCLE

Creation of image like below.
Image 
1. Take an UIImageView in .xib or storyboard file.
2. Link that name as you want. (Here i have taken imageView_AuthorPhoto)
Now code the below for make that in circular
imageView_AuthorPhoto.layer.masksToBounds = YES;
imageView_AuthorPhoto.layer.cornerRadius = imageView_AuthorPhoto.bounds.size.width/2;
imageView_AuthorPhoto.layer.borderWidth = 1.0;
imageView_AuthorPhoto.layer.borderColor =[UIColor colorWithRed:13/255 green:70/255 blue:131/255 alpha:1.0].CGColor;

And Set Image Accordingly
[imageView_AuthorPhoto  setImageWithURL:[NSURL   URLWithString:@"imageurl"] placeholderImage: useCache:<#(BOOL)#>];

Wednesday, September 18, 2013

Crop Image from an Image in Objective C

Here is the code to cut a piece from an Image or say UIImage

+ (UIImage *)cropImage:(UIImage *)image withArea:(CGRect)rect andScale:(int) scale{
    rect = CGRectMake(rect.origin.x*scale,
                      rect.origin.y*scale,
                      rect.size.width*scale,
                      rect.size.height*scale);
    
    CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], rect);
    UIImage *img = [UIImage imageWithCGImage:imageRef
                                       scale:scale
                                 orientation:image.imageOrientation];
    CGImageRelease(imageRef);
    return img;
}

Tuesday, July 30, 2013

Masking Image with a transparent PNG

Hi i have made a function in which you can pass 2 UIImages and it will mask with the transparent Image
like







- (UIImageView*) maskImage:(UIImage *)image withMask:(UIImage *)maskImage {
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    CALayer *mask1 = [CALayer layer];
    mask1.contents = (id)[maskImage CGImage];
    mask1.frame = CGRectMake(0, 0, 1024, 768);
    imageView.layer.mask = mask1;
    imageView.layer.masksToBounds = YES;
    return imageView;
}

Its returns the UIImageView, which you can add directly in UIView. You can call this method like following:

    UIImage *image = [UIImage imageNamed:@"screenFull.png"];
    UIImage *mask = [UIImage imageNamed:@"screen_mask.png"];
    [self.completeSingleView addSubview:[self maskImage:image withMask:mask]];

Here completeSingleView is a UIView.


Reference is taken from 
http://stackoverflow.com/questions/5757386/how-to-mask-an-uiimageview

Wednesday, January 30, 2013

Drag and Drop UIImageView(s) inside UIView

i have put a UIView inside the xib and made the IBOutlet for this as property name - sampleView.

This UIView's x and y position is as per my project. So no need to worry about the parent view x and y position. Because we can make it relative to superview controller as follow:

//imagesView is Mutable Array and contains all UIImages.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
    
    for(int i=0;i<imagesInView.count;i++)
    {
        UIImageView *imgView = [imagesInView objectAtIndex:i];
        if (CGRectContainsPoint([imgView frame], [touch locationInView:self.secondFullImageView])) {
            //if ([touch tapCount] == 2)
                //[imgView setupNextDisplayString];
            CGPoint touchPoint = [touch locationInView:self.sampleView];//Here you need to concentrate for your UIImageView Parent
            [UIView beginAnimations:nil context:NULL];
            [UIView setAnimationDuration:0.3];
            imgView.center = touchPoint;
            CGAffineTransform transform = CGAffineTransformMakeScale(1.2, 1.2);
            imgView.transform = transform;
            [UIView commitAnimations];
            return;
        }
   }
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [[event allTouches] anyObject];
    for(int i=0;i<imagesInView.count;i++)
    {
        UIImageView *imgView = [imagesInView objectAtIndex:i];
        if (CGRectContainsPoint([imgView frame], [touch locationInView:self.sampleView])) {
            CGPoint location = [touch locationInView:self.sampleView];
            imgView.center = location;
        }
}
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [[event allTouches] anyObject];
    
    for(int i=0;i<imagesInView.count;i++)
    {
        UIImageView *imgView = [imagesInView objectAtIndex:i];
        if (CGRectContainsPoint([imgView frame], [touch locationInView:self.sampleView])) {
            //if ([touch tapCount] == 2)
            //[imgView setupNextDisplayString];
            [UIView beginAnimations:nil context:NULL];
            [UIView setAnimationDuration:0.3];
            CGAffineTransform transform = CGAffineTransformMakeScale(1, 1);
            imgView.transform = transform;
            [UIView commitAnimations];
            return;
        }
    }
}

This Code is not for Zoom, Pan or Rotate. For these features you can go here
http://varunxcode.blogspot.in/2013/01/simple-drag-drop-with-zoom-and-rotate.html