iOS "resizableImageWithCapInsets: resizingMode:UIImageResizingModeStretch" this image how to set capInsets? - ios

I must ensure that the arrows do not stretch. So how should I set capInsets?
Please check this image:

UIImage *originalImg = [UIImage imageNamed:#"yourimagename"];
//Either you can use this
//UIImage *resizeImg = [originalImg resizableImageWithCapInsets:UIEdgeInsetsMake(4, 50, 30, 50) resizingMode:UIImageResizingModeStretch];
//or you can use this
UIImage *resizeImg = [originalImg resizableImageWithCapInsets:UIEdgeInsetsMake(4, 214, 30, 55) resizingMode:UIImageResizingModeStretch];
//Before resize
self.original_Imageview.image = originalImg;
//after resize
self.resized_Imageview.image = resizeImg;
You can use this code to set your image
[This is output image][1]
[1]: http://i.stack.imgur.com/iJV9X.png

Related

how to set frame for cell.imageView.frame?

i tried this below code in cell for row...
if([cell.textLabel.text isEqualToString:#"Home"])
{
cell.imageView.image = [UIImage imageNamed:#"Home-50.png"];
cell.imageView.frame=CGRectMake( 10, 15, 20, 20 );
}
This is below code is in didselect row at Indexpath...
if ([[arrCell objectAtIndex:indexPath.row] isEqualToString:#"Home"]) {
cell.imageView.image = [UIImage imageNamed:#"Home1-50.png"];
cell.imageView.frame=CGRectMake( 10, 15, 20, 20 );
}
I am using this above code to display image in tableview cell and when cell is clicked Then Image change to another image...Images are Changing But,the frames are not applying...
No you can't do this because it is fixed layout of UITableViewCell.If you want to do this go with below option
OPTION 1: CustomCell
Through CustomCell you can set image view frame whatever you want.
OPTION 2: Programmatically creating image view add it to cell
UIImageView *img = [[UIImageView alloc] init];
img.image = [UIImage imageNamed:#"yourImageName.png"];
[img setFrame:CGRectMake( 10, 15, 20, 20)];
[[cell.contentView addSubview:img];];
Can be achieved by transform scalingļ¼š
cell.imageView?.image = UIImage(named: "icon_shareToFriends")
if let imageView = cell.imageView, imageView.transform == .identity {
imageView.transform = imageView.transform.scaledBy(x: 0.55, y: 0.55)
imageView.contentMode = .center
}

How to slicing image and stretchable in iOS

I need to stretch image right and left side centre half circle remain as it is. also need half circle in center
I have tried slicing concept and also tried below code
UIImage *image = self.imgBGBottom.image;
CGFloat capWidth = floorf(image.size.width / 2) - 50;
CGFloat capHeight = 0;
UIImage *capImage = [image resizableImageWithCapInsets:
UIEdgeInsetsMake(capHeight, capWidth, capHeight, capWidth)];
[self.imgBGBottom setImage:capImage];
but it is not working for me
Please help me. Thanks in advance.
You are using function, use - (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight instead - (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets.
Set top, left cap to stretch your image as described in following code.
Objective-C
UIImage *image = [UIImage imageNamed:#"test"];
CGFloat capTop = 50; // top cap to keep half round as is
CGFloat capLeft = 5; // To repeat it or stretch equally.
UIImage *capImage = [image stretchableImageWithLeftCapWidth:capLeft topCapHeight:capTop];
Swift
let image = UIImage(named: "stretchableImage")
let capTop:Int = 50; // top cap to keep half round as is
let capLeft:Int = 5; // To repeat it or stretch equally.
let capImage = image?.stretchableImage(withLeftCapWidth: capLeft, topCapHeight: capTop)
Alternet Solution
Same result can be achieved using following function as well.
Objective-C
UIImage *stretchedImage = [image resizableImageWithCapInsets:
UIEdgeInsetsMake(50, 50, 0, 50)];
Swift
var stretchedImage = image?.resizableImage(withCapInsets: UIEdgeInsets(top: 50, left: 50, bottom: 0, right: 50), resizingMode: .stretch)
Note : Keep stretchable image as small as possible otherwise it will not stretch properly with smaller image container(ImageView, Button etc.). You can reduce height & width of your existing image.
try this
UIImage *image = self.imgBGBottom.image;
CGFloat capWidth = floorf(image.size.width / 2) - 50;
CGFloat capTop = 50; //the value is determined by the half circle height,i guess it is 50 height
CGFloat capBottom = 0;
UIImage *capImage = [image resizableImageWithCapInsets:
UIEdgeInsetsMake(capHeight, capTop, capBottom, capWidth)];
[self.imgBGBottom setImage:capImage];

IOS UIImageView in cell has wrong size

I'm trying to replace an Image from an URL if it exists.
The problem seems to be that I can't set the size of my UIImageView.
My Code looks like this:
UIImageView *partnerIcon = (UIImageView*)[cell viewWithTag:0];
NSURL *imageUrl = [NSURL URLWithString:[[#"http://www.fitpas.ch/coreapp/resources/images/center/" stringByAppendingString:[[result objectAtIndex:indexPath.row] objectForKey:#"cid"]] stringByAppendingString:#".jpg"]];
UIImage* partnerImage = [UIImage imageWithData: [NSData dataWithContentsOfURL: imageUrl]];
if (partnerImage != nil) {
dispatch_async(dispatch_get_main_queue(), ^{
//change image
partnerIcon.image = partnerImage;
partnerIcon.contentMode = UIViewContentModeScaleToFill;
});
}
This results in:
In the Image above the last row partnerImage is nil and that shows how it should be.
I tried to scale the UIImage with
partnerIcon.image = [UIImage imageWithCGImage:partnerImage.CGImage
scale:1/100 orientation:partnerImage.imageOrientation];
but this won't change anything.
I also tried to set to change the dimension of the UIImageView with:
partnerIcon.bounds = CGRectMake(0, 0, 50, 50);
and also
partnerIcon.frame = CGRectMake(0, 0, 50, 50);
but this isn't working either.
Set an explicit width and height constraint on the image view. If you don't do this then the content hugging and compression values will be used across all of the views to decide how big each should be based on the content size.

Where to add image layer for xcode objects?

I want to add a border around my UIimageView?
Let's say everytime I add a new photo:
flagImageView.image = UIImage(named: correctAnswer)
the photo is different, and I need to re-scale.
Where do I put the code, although its not the right answer. I am unsure how to rescale the imageview
// Define a new image object
UIImage *image = [UIImage imageNamed:#"lake.png"];
// Define a new image view
UIImageView* imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 157, 125)];
// Set image in imageview and add to view
[imgView setImage:image];
[[self view] addSubview:imgView];
// Create a white border with defined width
imgView.layer.borderColor = [UIColor whiteColor].CGColor;
imgView.layer.borderWidth = 1.5;
// Set image corner radius
imgView.layer.cornerRadius = 5.0;
// To enable corners to be "clipped"
[imgView setClipsToBounds:YES];
I have a code that adds a new image, with different size to a UIimageView. How would I add an image? Is that in a subclass, or viewController, or model?
this would be the code in swift:
override func viewDidLoad() {
super.viewDidLoad()
// Define a new image object
let image = UIImage(named: "massage.jpg")
// Define a new image view
let imgView = UIImageView(frame: CGRect(x: 40, y: 40, width: 157, height: 125))
// Set image in imageview and add to view
imgView.image = image
self.view.addSubview(imgView)
// Create a white border with defined width
imgView.layer.borderColor = UIColor.redColor().CGColor
imgView.layer.borderWidth = 1.5
imgView.layer.cornerRadius = 5.0;
imgView.layer.masksToBounds = true
// Do any additional setup after loading the view.
}
this is how it would look like:
viewDidLoad is called when the controller loads it's view, which is not necessarily right after initialisation.
viewWillAppear is called just before the view is displayed.viewWillAppear is called every time the view is displayed;

move didFinishLaunchingWithOptions: method to viewcontroller method with button trigger

I had the following code working in AppDelegate.h/.m but cannot change it to work in ViewController with a button triggering it.
#implementation BSViewController
#synthesize imageView;
#synthesize workingImage;
- (IBAction) chooseImage:(id) sender {
UIImage* testCard = [UIImage imageNamed:#"ipad 7D.JPG"];
CGImageRef num = CGImageCreateWithImageInRect([testCard CGImage],CGRectMake(532, 0, 104, 104));
UIGraphicsBeginImageContext(CGSizeMake( 250,650));
CGContextRef con = UIGraphicsGetCurrentContext();
UIImage* im = UIGraphicsGetImageFromCurrentImageContext();
CGContextDrawImage(con, CGRectMake(0, 0, 13, 13) ,num);
UIGraphicsEndImageContext();
CGImageRelease(num);
UIImageView* iv = [[UIImageView alloc] initWithImage:im];
[self.imageView addSubview: iv];
iv.center = self.imageView.center;
[iv release];
I see the button named "Choose image" in the simulator, but I do not see the image there.
You haven't added self.imageView to any views.
You need
[self.view addSubview:self.imageView];
To show your imageView.
I was able to make it work, but let me ask you some dummy questions before. You can skip these and jump directly to the end for the answer:
Did it worked before?
Why aren't you using ARC?
Have you linked the chooseImage: to the button with Interface Builder, or programmatically? I sometimes forget this basic step, and nothing works of course :)
Why are you adding iv as a subview of self.imageView instead of just changing the image property of self.imageView. Knowing this could be useful to answer properly.
Supposing you have a good reason for adding iv as a subview, beware iv.center = self.imageView.center the centers are in two different coordinates system.
Form the Apple Documentation:
The center is specified within the coordinate system of its superview
and is measured in points. Setting this property changes the values of
the frame properties accordingly.
Edit: Other things to check
Does self.imageView exist? I mean, have you added it with IB and linked it to the IBOutlet, or defined it programmatically?
Add a break point in chooseImage: and go through it step by step, making sure that the objects you generate there are not nil
Test: try self.imageView setImage:im instead of self.imageView addSubview:iv, even if it's not what you want to do, if everything else is ok you should see the image.
It works if you switch the order of
UIImage* im = UIGraphicsGetImageFromCurrentImageContext();
and
CGContextDrawImage(con, CGRectMake(0, 0, 13, 13) ,num);
Otherwise you would be asking to create im with the content of the context without nothing in drawn in it.
The code would then become:
UIImage* testCard = [UIImage imageNamed:#"ipad 7D.JPG"];
CGImageRef num = CGImageCreateWithImageInRect([testCard CGImage],CGRectMake(532, 0, 104, 104));
UIGraphicsBeginImageContext(CGSizeMake( 250,650));
CGContextRef con = UIGraphicsGetCurrentContext();
CGContextDrawImage(con, CGRectMake(0, 0, 13, 13) ,num);
UIImage* im = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGImageRelease(num);
Hope it helps. :D

Resources