BackgroundColor + image of UIImageView to UIImage - iphone

I have a UIImageView with some lines (drawed by hand) and a background color. When i put the image of the UIImageView into a UIImage and save it to the photo library the background is white again. Is there any way to put the image property and the backGroundColor property of a UIImageView into one UIImage ?

It is possible to render the contents of an arbitrary UIView to an UIImage.
See http://www.icab.de/blog/2010/10/01/scaling-images-and-creating-thumbnails-from-uiviews/ for an implementation.
At the bottom of that page is a category for UIImage which, when imported, simply lets you create the UIImage from the UIView with UIImage *img = [UIImage imageFromView:someView];

Related

How to set up a default image

Before i click the image to pull the camera, i want to set up a default image on the imageview that shows a http://www.inc.com/images/avatar/default1.gif to inform that in that imageview goes a person image.
What is the best way to do this procedure?
Best regards.
If you are using a UIImageView the first thing is to create a UIImage object using this code
UIImage *defaultImage = [UIImage imageNamed:#"default.png"];
And then pass it to the UIImageView object if it is a property of your view controller you can use this(imageView is the variable name of UIImageView)
self.imageView.image = defaultImage;
If you are instantiating a new UIImageView in your view controller you can use this
UIImageView *imageView = [[UIImageView alloc] initWithImage:defaultImage];
UIImageView *theImageView;
//assuming that's the declaration in your header
//make sure default1.gif is inside your application bundle
//copied into the resources folder of your xcode project
//this code goes into your -viewDidLoad method
theImageView.image = [UIImage imageNamed:#"default1.gif"];

Programmatically prevent UIImageView from autoresizing

I would like to truncate one end of a UIImageView by changing the size of the frame dynamically. Of course, if it autoresizes it won't look 'truncated', but will look shrunk. The former is the effect I would like, the latter not.
So far I've set up the UIImageView in IB and then hooked it up programmatically using IBOutlet. I then placed this in ViewDidLoad, but still get the autoresizing:
[self.stringImageView setAutoresizingMask:UIViewAutoresizingNone];
self.stringImageView.frame = CGRectMake(9.0f, 508.0f, 500.0f, 26.0f);
Any ideas how I can clip my UIImageView instead of having it resize?
EDIT 1
It should also be noted that in IB I have deselected Clip Subviews and Autoresize Subviews.
Use : stringImageView.contentMode = UIViewContentModeCenter; so the image you add to the UIImageView always keeps the same size and the same position in your view.
You need to clip the UIImage object first according to imageview's frame and then set it as image for image view. See also Cropping an UIImage
#implementation UIImage (Resize)
// Returns a copy of this image that is cropped to the given bounds.
// The bounds will be adjusted using CGRectIntegral.
// This method ignores the image's imageOrientation setting.
- (UIImage *)croppedImage:(CGRect)bounds {
CGImageRef imageRef = CGImageCreateWithImageInRect([self CGImage], bounds);
UIImage *croppedImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return croppedImage;
}
In Interface Builder set Mode in the View Tab to "Top" or whatever it should be.
Another more efficient way to go if you only wish to make part of the image visible,
or for example animate its appearance,
is to set the content mode of the UIImageView to UIViewContentModeCenter (or any other alignment) and include it in a UIScrollView.
You will only need to change the frame of the scroll view and your image will be visually cropped without you having to create the cropped image (which would be a very high price if it is for animation purpose).

How to convert UIView to UIImage without background?

I have UIView that contain a pin image and a label, as we know UIView is rectangle so if I convert UIView to UIImage,UIImage is also rectangle, I want make UIImage like the a pin image because if user click a background, UIImage's event will be called too.. I want to convert UIView to UIImage without it's background, how can it be?
I use this method to change UIView to UIImage
-(UIImage *) ChangeViewToImage : (UIView *) view{
UIGraphicsBeginImageContext(view.bounds.size);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
Hmm.. example like this, I have a rectangle as a UIView and a triangle as a shape in UIView. and then I want to make this UIView become a UIImage, but I just want the triangle without background, so the image just a triangle.. how can I do It?
A UIImage is always rectangular. What you can play with is the opacity in your views. If you make everything except your pin image transparent, it will seem as if you just have an image of the pin.
Use:
view.backgroundColor = [UIColor clearColor];
to make your view's background transparent.

regarding UIImageView Constant Animation

I have an image I want to animate constantly in UIImageView. Like some image object to move left and right?
Thanks in advance.
Create the animation in a image editor (or something else if you already have the animation)
Then save each frame as an image. Create a folder in your Xcode project and import the files into it. (in the example below each frame is named "frame-x.png" where x is the frame number)
Then create an array of UIImages and set the UIImageView's animationImages property to it:
imageView.animationImages = [NSArray arrayWithObjects:[UIImage imageNamed:#"frame-1"],
[UIImage imageNamed:#"frame-2"],
[UIImage imageNamed:#"frame-3"],
[UIImage imageNamed:#"frame-4"],
nil];
Where you have a UIImage for each frame. You then start the animation:
[imageView startAnimation];
You can also set the animationRepeatCount property of the image view.
See the UIImageView Class Reference for more details.

iPhone: Adding UIImage to UIImageView adds a second image

I have a UIImageView that I have already set an image to. This works fine, but later, I want to change that image. So, I set a new image for the image property, but when I view the app, that image is set as a second image over the first:
UIImageView *image;
image = (UIImageView *)[cell viewWithTag:0];
image.image = [UIImage imageNamed:#"History_Row2.png"];
How can I replace the current image in my UIImageView rather than seeming to add another?!
I did it by retaining the UIImageView, and keeping a reference as an ivar, releasing on dealloc.
image = (UIImageView *)[cell viewWithTag:0];
That was returning a UITableViewCell rather than a UIImageView, this is because the tag 0 is the default and it needs to be changed to a different number. After doing this it worked.