Objective-C - How to use UIImageView on ios develop

tag: Objective-C    date: 2018-10-14

create a new uiimageview for test

UIImageView *imageView = [[UIImageView alloc] initWithFrame:(CGRect){10,50,300,200}];
imageView.image = [UIImage imageNamed:@"test.png"];
[self.view addSubview:imageView];

uiimageview set border and border color

imageView.layer.borderWidth = 4.0f;
imageView.layer.borderColor = [UIColor blackColor].CGColor;

uiimageview set rounded corners - corner radius

imageView.layer.cornerRadius = 8.0f;
imageView.layer.masksToBounds = YES;

uiimageview set content mode

imageView.layer.masksToBounds = YES;
imageView.contentMode = UIViewContentModeScaleAspectFit;  // you can try other mode
Here is the result image:

uiimageview set alpha value

imageView.alpha = 0.5;

uiimageview set blur effect

UIVisualEffectView *blurMask = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
blurMask.frame = imageView.frame;
[self.view addSubview:blurMask];

uiimageview show grayscale image

UIImage *image = imageView.image;
CGRect imageRect = CGRectMake(0, 0, image.size.width, image.size.height);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
CGContextRef context = CGBitmapContextCreate(nil, image.size.width, image.size.height, 8, 0, colorSpace, kCGImageAlphaNone);
CGContextDrawImage(context, imageRect, [image CGImage]);
CGImageRef imageRef = CGBitmapContextCreateImage(context);
UIImage *newImage = [UIImage imageWithCGImage:imageRef];
CGColorSpaceRelease(colorSpace);
CGContextRelease(context);
CFRelease(imageRef);
imageView.image = newImage;

uiimageview load image from url

imageView.image = [UIImage imageWithData:[[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"https://xxx.jpg"]] scale:1];

rotate uiimageview 90 degree

[imageView setTransform:CGAffineTransformMakeRotation(-M_PI / 2)];

uiimageview handle touch event

imageView.userInteractionEnabled = YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
[imageView addGestureRecognizer:tap];
-(void) tap:(UITapGestureRecognizer*)gesture{
[[[UIAlertView alloc] initWithTitle:@"demo"
                            message:@"UIImageView Taped"
                           delegate:self
                  cancelButtonTitle:@"cancel"
                  otherButtonTitles:nil] show];
}

uiimageview set circle mask

imageView.layer.borderWidth = 8.0f;
imageView.layer.borderColor = [UIColor blackColor].CGColor;
imageView.layer.cornerRadius = imageView.bounds.size.width/2;
imageView.layer.masksToBounds = YES;

uiimageview change tint color - tintcolor

imageView.image = [UIImage imageNamed:@"icon.png"];
imageView.image = [imageView.image imageWithRenderingMode:(UIImageRenderingModeAlwaysTemplate)];
imageView.tintColor = [UIColor redColor];

uiimageview set blend mode - blend image with color

UIImage * image = imageView.image;
UIColor * color = [UIColor colorWithRed:0.295 green:0.505 blue:1.000 alpha:1.0];
UIGraphicsBeginImageContext(image.size);
[image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height) blendMode:kCGBlendModeNormal alpha:1];
UIBezierPath * path = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, image.size.width, image.size.height)];
[color setFill];
[path fillWithBlendMode:kCGBlendModeMultiply alpha:1];
UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
imageView.image = newImage;

uiimageview fade between two images

UIImage * toImage = [UIImage imageNamed:@"icon.png"];
[UIView transitionWithView:imageView
                  duration:2.f
                   options:UIViewAnimationOptionTransitionCrossDissolve
                animations:^{
                    imageView.image = toImage;
                } completion:NULL];

more articles
Copyright © 2018
This Site is Licensed under a Creative Commons License. Powerd by Java