I need to create an UITextView like in Image, but UITextView auto resize to content height. It is in chat message. At left-bottom and right-bottom have one arrow. But I don't know how to draw or create them in iOS. Can someone tell me how to do this? thank you very much.
Arrow in left-bottom and turn the left if messsage from another body, arrow on the right-bottom and turn the right when message from "self"
You can use the mirror effect On the image. by using same image for both sender and receiver
-(UIImage*)setBubbleImageForSender
{
UIImage *image = [UIImage imageNamed:#"sender.png"];
return image;
}
-(UIImage*)setBubbleImageForReceiver
{
UIImage *image = [UIImage imageNamed:#"sender.png"];
image = [UIImage imageWithCGImage:image.CGImage
scale:image.scale
orientation:UIImageOrientationUpMirrored];
return image;
}
The above thing will change the arrow direction for the chat app.
for resizing the height and width you can use the [imgvw resizableImageWithCapInsets:..] for the imageview and set table cell height accordingly.
Thanks
Related
I am setting images to a UIImage programmatically. But I want to also add a play button over that view. Think of Facebook in how there is a play button over the image. That is exactly what I am trying to figure out on what to do. Here is my setting of the UIImage:
UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:#"%d.jpg", indexPath.row]];
cell.photoView.image = img;
Suggestions, thoughts?
You can add playbutton either as a UIButton or a UIImageView and add as a subview to your photoview.
[cell.photoView addSubview:urPlayButtonView];
I'm going to use rounded corner box as 'container' of some UIComponents and, of course, it will requires multiple width and height.
my question is... is there any way to have just one PNG file with rounded corner but programatically stretch some area horizontally or vertically so it can be used in multiple UIView. if so, what UIComponent will be used to hold this image? is it UIImageView?
here's what I mean :
You can use below method to stretch imageview :
UIImage *img = [UIImage imageNamed:#"imageName"];
img = [img stretchableImageWithLeftCapWidth:10 topCapHeight:10];
and then its img into imageview.
I have an oversized image loaded in a image view that goes out of bounds both vertically and horizontally.
The end user can scroll around the image (the oversized imageview is in a scrollview) and when they find an area that they like I would like to crop out the area of the image that is shown on the screen. (much like a screenshot but only of the imageview.image I'm then going to put that into a different Imageview.
I can't seem to work out how to accomplish the "screenshot" of the area of the image view's image that is currently showing on the screen.
You can use CGImageCreateWithImageInRect to create a subimage of the displayed image. Use contentOffset and the scrollViews bounds to create the rect from which you want to create the image.
CGRect rect = CGRectMake(scrollView.contentOffset.x, scrollView.contentOffset.y, CGRectGetWidth(scrollView.bounds), CGRectGetHeight(scrollView.bounds));
CGImageRef subImageRef = CGImageCreateWithImageInRect([originalImage CGImage], rect);
If you zoom your scrollView you will need to take the zoomLevel into account too.
I ended up using the following code to achieve what I was looking for to grab the image. Thank you to Karl for his input and a thank you to iNoob whom answer to a previous question [Located here on StackOverflow][1] I used for mine.
Just use the below code to take a "screenshot" just set anything you don't want in the image to.hidden = True; before the code to hide it from the screenshot and set them to .Hidden = FALSE; after the code to bring them back.
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, self.view.opaque, 0.0);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
I have a view with two UIImageViews. The first uiimageview is a picture, which can be changed but is always at the same position. The second uiimageview contains a uiimage of a logo, this uiimageview can be resized, panned and rotated.
Both uiimageviews are set to Aspect Fit by the way.
When im done dragging and scaling the second uiimageview, i want to save these two uiimages, and draw one single uiimage. But in the new uiimage i need them to be positioned exactly as they were after i finished dragging and resizing the second uiimageview.
Im pushing this combined uiimage over to a new uiviewcontroller, which is called PreviewViewController. Here i want to show the uiimage in an imageview, and save it if the user presses yes.
I almost got it working, but the x and y position is confusing me.
And there is another problem, when drawin the second uiimageview image onto the new image, its view mode looks like Scale to fill and it looks ugly.
Heres my code
- (UIImage *)combineImages{
UIImage *tshirt = self.tskjorteTemplateView.image;
UIImage *logo = self.bildeView.image;
UIGraphicsBeginImageContext(self.tskjorteTemplateView.image.size);
UIGraphicsBeginImageContextWithOptions(self.tskjorteTemplateView.frame.size, NO, 0.0);
[tshirt drawInRect:CGRectMake(0,0, self.tskjorteTemplateView.frame.size.width, self.tskjorteTemplateView.frame.size.height)];
[logo drawInRect:CGRectMake(bildeView.center.x - (bildeView.frame.size.width / 2) ,
bildeView.center.y - (bildeView.frame.size.height / 2), self.bildeView.frame.size.width, self.bildeView.frame.size.height)];
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return result;
}
and its called here:
fullPreviewImage = [self combineImages];
WantToSaveViewController *save = (WantToSaveViewController *)segue.destinationViewController;
save.delegate = [self.navigationController.viewControllers objectAtIndex:0];
save.previewImage = fullPreviewImage;
Behold:
//UIImageView* stretchTest = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"image.png"]];
//[self addSubview:stretchTest];
UIButton *stretchTest = [UIButton buttonWithType:UIButtonTypeCustom];
[stretchTest setFrame:CGRectMake(0, 0, 400, 100)];
[stretchTest setBackgroundImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
[self addSubview:stretchTest];
stretchTest.contentStretch = CGRectMake(0.5, 0.5, 0, 0);
stretchTest.contentMode = UIViewContentModeScaleToFill;
CGRect frame = stretchTest.frame;
frame.size.height = 300;
stretchTest.frame = frame;
Using the UIImageView (commented out above), the image stretches appropriately - rounded corners maintain the correct radius, because only the center pixel of the image gets stretched.
Using the UIButton, the image gets stretched incorrectly. The corner radii are not maintained and it gets ugly.
Both UIImageView and UIButton are subclasses of UIView. Why does the button resize differently than the imageView?
You're making assumptions about the way UIButton works. It's not implemented the same way as UIImageView. UIImageView is just a view, with no subviews, and its contents is the image. UIButton is different, and the way it works is a private implementation detail.
If you're trying to stretch an image appropriately on a button, you should use -[UIImage stretchableImageWithLeftCapWidth:topCapHeight:] to get a UIImage that knows how it should be stretched. If you want to stretch just the middle pixel, you can use something like
UIImage *image = [UIImage imageNamed:#"image.png"];
image = [image stretchableImageWithLeftCapWidth:floorf(image.size.width/2) topCapHeight:floorf(image.size.height/2)];
A UIButton has two types of images it can display -- a foreground image and a background image. The background image for a button is expected to replace the button's background texture. As such, it will stretch to fill the entire background. The button's foreground image is expected to be an icon that may or may not display alongside text; it will not stretch. It may shrink if the frame is smaller than the image, but it will not stretch.
A button's foreground and background image can be set in code like this:
// stretchy
[self setBackgroundImage:backgroundImage forState:UIControlStateNormal];
// not stretchy
[self setImage:forgroundImage forState:UIControlStateNormal];
By default, the backgroundImage of a button will use scaleToFill to stretch the image. If you need the image to stretch using cap insets though, you should set them on the image before assigning it to the backgroundImage, like this:
UIImage *image = [UIImage imageNamed:#"bg_image.png"];
/* This assumes your image will have a 1px column and 1px row of pixels
in the horizontal and vertical middle of the image that should be
stretchable. If that's not the case (such as asymetrical buttons)
you need to adjust the caps */
image = [image stretchableImageWithLeftCapWidth:floorf(image.size.width/2)
topCapHeight:floorf(image.size.height/2)];
[self setBackgroundImage:image forState:UIControlStateNormal];