Pages

Showing posts with label UIImage. Show all posts
Showing posts with label UIImage. 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;
}

Friday, September 13, 2013

Load Image from different Sources

Load Image from External URL

NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://someimageurl/imagename.jpg"]];
    
    
UIImage *image = [[UIImage alloc] initWithData:data];
    
Load Image from Project Itself
    

UIImage *image = [UIImage imageNamed:@"Disp1.jpg"];

Saturday, August 3, 2013

Add Image from URL in UIView and make clickable

I have made a sample code for this example

Step 1 - Load Image from URL

     UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"screen.png"]];

  [self.completeSingleView addSubview:imageView];

Step 2 - Now to make this clickable just use the following code

    imageView.userInteractionEnabled = YES;
   imageView.tag = 2;

Step 3 - Now to add this in any UIView. Follow the code below

     [self.completeSingleView addSubview:imageView];

Step 4- Now to detect the Image in touch began event, follow the code below


-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    
    UITouch *touch = [touches anyObject];
    if([touch.view tag] == 2)
        NSLog(@"Your Image is Touched");
}

Wednesday, July 31, 2013

Animate Mask in Objective C

First of all you can view my masking image logic here

View

Now If i click the screen and want to animate this mask, then we can apply the below logic


-(void)moveLayer:(CALayer*)layer to:(CGPoint)point
{
    // Prepare the animation from the current position to the new position
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
    animation.fromValue = [layer valueForKey:@"position"];
    animation.toValue = [NSValue valueWithCGPoint:point];
    animation.duration = .3;
    layer.position = point;
    
    [layer addAnimation:animation forKey:@"position"];
}

-(void)resizeLayer:(CALayer*)layer to:(CGSize)size
{
    CGRect oldBounds = layer.bounds;
    CGRect newBounds = oldBounds;
    newBounds.size = size;
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"bounds"];
    
    animation.fromValue = [NSValue valueWithCGRect:oldBounds];
    animation.toValue = [NSValue valueWithCGRect:newBounds];
    animation.duration = .3;
    layer.bounds = newBounds;    
    [layer addAnimation:animation forKey:@"bounds"];
}

- (UIImageView*) animateMaskedImage:(UIImage *)image withMasked:(UIImage *)maskImage width:(int )wd andHeight:(int )ht andSize:(CGSize)size andPosition:(CGPoint)pt {
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    CALayer *mask = [CALayer layer];
    mask.contents = (id)[maskImage CGImage];
    mask.frame = CGRectMake(0, 0, wd, ht);
    //Masking Animation
    [self resizeLayer:mask to:size];
    [self moveLayer:mask to:pt];
    
    imageView.layer.mask = mask;
    imageView.layer.masksToBounds = YES;
    return imageView;
}

Now we just need to call animatedMaskedImage function in our touch event like below

        [self.imageView1 removeFromSuperview];
        self.imageView1 = [self animateMaskedImage:self.image1 withMasked:self.mask1 width:1024 andHeight:768 andSize:CGSizeMake(1024*4, 768*4) andPosition:CGPointMake(1024*2, 768*2)];
        [self.completeSingleView addSubview:self.imageView1];

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

Take Bitmap from UIView

You can capture UIImage from any UIView by following function

*Note: Import the following class to use UIView layer properties 
 #import <QuartzCore/QuartzCore.h>

Code is :


+ (UIImage*)imageFromView:(UIView*)view
{
    [self beginImageContextWithSize:[view bounds].size];
    BOOL hidden = [view isHidden];
    [view setHidden:NO];
    [[view layer] renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    [self endImageContext];
    [view setHidden:hidden];
    return image;
}

+ (UIImage*)imageFromView:(UIView*)view scaledToSize:(CGSize)newSize
{
    UIImage *image = [self imageFromView:view];
    if ([view bounds].size.width != newSize.width ||
        [view bounds].size.height != newSize.height) {
        image = [self imageWithImage:image scaledToSize:newSize];
    }
    return image;
}

+ (UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize
{
    [self beginImageContextWithSize:newSize];
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    [self endImageContext];
    return newImage;
}

Above Reference is taken from :

Sunday, January 27, 2013

Create UIView and work with UIImage

First Create UIView by following Coding



UIView *uview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 600, 600)];
[viewControl addSubview:uview];

Now to load the Image from External source is as follow:

   NSURL * imageURL = [NSURL URLWithString:@"http://ww.url.com/imageName.jpg"];
    NSData * imageData = [NSData dataWithContentsOfURL:imageURL];
    UIImage * image = [UIImage imageWithData:imageData];
    UIImageView *block = [[UIImageView alloc] initWithImage:image];
    block.frame = CGRectMake(10, 10, 200, 200);
    [uview addSubview:block];
    block = nil;
    uview = nil;
    image = nil;
    imageData = nil;
    imageURL = nil;
    

I have used ARC - Automatic Reference counting. So didn't released the variable here.