Combining images - iphone

I want to place the picked image on top of another image, so that my picked image will be placed in some sort of frame I made in Photoshop. After combining the images I want to save it to the disk.
Does anyone know how to do it, or maybe have a link to examples?

You can just layer the two images on top of each other, first add the frame image, then add the image with the photo...
Heres sample code
Assuming your 2 images are already sized correctly to fit one on top of t he other, this code would be in a view controller
UIImageView *frame=[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Frame.png"]];
UIImageView *pic=[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"pic.png"]];
frame.center=[self.view center];
pic.center=[self.view center];
[self.view addSubview:frame];
[self.view addSubview:pic];
here it is, memory managment has not been written in..

You can also add the picture UIImageView directly the the frame UIImageView: same as Daniel's suggestion above, but [frame.view addSubview: pic] instead.

Related

UIImageView not showing the background View if the image has transparent regions

I have a UIView has some labels and buttons on it.
Next I also have a image which has a square area that is transparent, now
if I create a UIImageView and add this image which has transparent regions I am not able to see the background view (which has buttons and labels) through this transparent image.
If I play with the alpha value that doesn't work as intended which is to see the transparent regions exactly as it would have appeared on the UIView which has the labels and buttons.
UIImage* image = [UIImage imageNamed:#"TI1.jpg"];
UIImageView* imageView = [[UIImageView alloc] initWithImage:image];
[self.view addSubview:imageView];
Also I would be interested to know if there is other way to achieve what I am trying to achieve.
Basically I want to highlight a certain area of the view which has buttons/labels and make the rest of the area greyed out. My idea was to have this UIImageView with transparent regions in image to achieve that.
Thanks
Ankur
Try setting imageView.opaque = NO;
UIImageView inherits from UIView. According to that class's docs:
This property provides a hint to the drawing system as to how it
should treat the view. If set to YES, the drawing system treats the
view as fully opaque, which allows the drawing system to optimize some
drawing operations and improve performance. If set to NO, the drawing
system composites the view normally with other content. The default
value of this property is YES.
Also, not sure that JPG even supports transparency, so try exporting the image as a PNG to ensure you get the results you're looking for.

Setting a jpg as the background for a UIView?

I know this is a very beginner question, but I'm obviously a beginner. I have already made a my TabBar but I want to set the background of one of the views as a (jpg) I created. I need to add the background in code (not IB) so that I can allow rotation and resizing when the iphone is rotated.
-thanks
You need to use a UIImageView, which is a subclass of UIView. You can create it as follows:
UIImageView *myImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"myImage.jpg"]];
[self.view addSubview:myImage];
[myImage release];
...so here I've created a UIImageView that uses a JPG called 'myImage' (it will automatically resize the view to fit the image), added it to my view controller, and then cleaned up my memory.
You can try this one to set an image as a background for a view programmatically:
view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"imageName.png"]]
Add an instance of UIImageView to your view, and set it's image to an instance of UIImage created using your file.
You can add the image view in IB or in code -- there's nothing about doing it in IB that would prevent you from resizing, rotating, etc.

image display in uiscrollview

Am facing a probem with my scrollview.Am displaying the image in a scrollview programatically and i added the zooming and paning effects.When i run the application image is displaying in the scroll view but it is not in centre(like it is displaying at right corner and some part of image is inside the scrollview)After zooming when i scroll the image the image is moving and i can see the black area which i dont want.
Please,somebody can help me
Thanks in advance
here is the code
mainView2 = [[UIScrollView alloc]init];
[mainView2 setFrame:CGRectMake(imageView2.frame.origin.x, imageView2.frame.origin.y, 222, 120)];
[mainView2 setContentSize:CGSizeMake(600,600)];
[mainView2 setMaximumZoomScale:2.0];
[mainView2 addSubview:imageView2];
mainView2.delegate = self;
[self.view addSubview:mainView2];
mainView2.scrollsToTop = NO;
here mainview2 is my scrollview and imageview2 is the uiimageview which i want to display in scrollview
I don't know exactly what you mean "I can see the black Area". However, the "center image" problem is quite a common one. As far as I know, there is no out of the box way to solve this. Fortunately, there is some great code on github which handels image scrolling in a UIScrollView nicely. See this question and this github project for further information.

How do I resize a picture using UIImageView so that I can zoom in and out?

In my view I have a picture.. The picture has a 3:2 scale and is huge, resolution wise.
I'm having a lot of trouble initializing a UIImage with the image to the original size and then zooming out of the UIImageView so that the entire image is visible within the scrollview.
The program stays only in the portrait orientation.
And no, please don't say to just use UIWebView. UIWebView doesn't let me set the content size during view load...instead, it just zooms out of the image by some arbitrary scale and I couldn't figure out a way to adjust the scale value (I don't think it's possible).
Thanks for any responses! I really appreciate them :D
Here's an example of placing an image that responds to pinch-to-zoom. Basically, you place the UIImageView in a UIScrollView and change some settings.
UIImageView *myImage;
UIScrollView *myScroll;
-(void)viewDidLoad{
myImage = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,360,450)];
[myImage setImage:[UIImage imageNamed:#"coolpic.png"]];
myImage.contentMode = UIViewContentModeScaleAspectFit;
[myScroll addSubview:myImage];
[myScroll setContentSize:CGSizeMake(myImage.frame.size.width, myImage.frame.size.height)];
[myScroll setMinimumZoomScale:1.0];
[myScroll setMaximumZoomScale:4.0];
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
return myImage;
}
Of course, set all your delegates and IB hooks properly.
Edit: I just reread your question. The portion of the example that answers your question is the frame specification of the UIImageView and its contentMode setting.
You almost certainly don't want to load your 'huge, resolution wise' image all at once on load regardless of it's scale. I'd suggest checking out some of Apple's sample code on this stuff (starting with ScrollViewSuite would be good, I'd say).
There was also a recent video released from WWDC where they implement this sort of thing live (they have more of a photo viewing app, but you could pretty easily do what they show with just one image too) so take a look for that too.
Go to the attributes tab of the UIImageView. Select "aspect to fill" or "aspect to fit". If you use "aspect to fill", you might not show the whole image. I just use "aspect to fit", and make the background black.

Layers of Images using UIImageView

I just wanted to confirm, is it possible that we add layers of images using UImageView, one as background and other on top of it. I mean i want to use 2 images one has to be in the background and the other on top of it. ( the size of both these images is 320 x 480 ).
Through the attributes inspector we cant do it. Is there any we can do it through code.
Maybe using subview or something.
You need one UIImageView for each image, but they will appear layered as long as the top view is not opaque. You could put both image views into a third UIView if you want to treat them as a single entity.
Edit:
imageBackground = [[UIImageView alloc] initWithImage:[UIImageNamed:#"background.png"]];
imageForeground = [[UIImageView alloc] initWithImage:[UIImageNamed:#"foreground.png"]];
imageLayered = [[UIView alloc] initWithFrame:[imageBackground frame];
[imageLayered addSubview:imageBackground];
[imageLayered addSubview:imageForeground];
// release all views at some point.
imageLayered is now a UIView with a background and foreground image.