I'm sure this is simple but I am trying to add an array of images to a layer. Here is what I have so far:
// Create the fish layer
fishLayer = [CALayer layer];
//fish = [UIImageView imageNamed:#"Fish.png"];
fish.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"swim01.png"],
[UIImage imageNamed:#"swim02.png"],
[UIImage imageNamed:#"swim03.png"],
[UIImage imageNamed:#"swim04.png"],
[UIImage imageNamed:#"swim05.png"],
[UIImage imageNamed:#"swim06.png"],
[UIImage imageNamed:#"swim05.png"],
[UIImage imageNamed:#"swim04.png"],
[UIImage imageNamed:#"swim03.png"],
[UIImage imageNamed:#"swim02.png"], nil];
fish.animationDuration = 1.50;
fish.animationRepeatCount = -1;
[fish startAnimating];
//[self.view addSubview:fish];
//This should add the animated array to layer.
fishLayer.contents = fish;
fishLayer.bounds = CGRectMake(0, 0, 56, 56);
fishLayer.position = CGPointMake(self.view.bounds.size.height / 2,
self.view.bounds.size.width / 2);
[self.view.layer addSublayer:fishLayer];
There is no error but the array of images don't appear on the screen. I think maybe this line is the probem..
fishLayer.contents = fish;
I have added the imageview to my header files and added it in the XIB
Please help if you can,
Cheers,
Adam
Reading your code, it looks like you're not initializing fish. Since you're not getting any errors, I assume it is an instance variable, which means it gets set to nil initially. So when you set fish.animationImages, you essentially do nothing (fish is nil). Same with every other use of fish in this code snippet.
It looks like you were using views initially, but then commented all that out. Why are you trying to use a layer? You should be able to just do this:
fish = [UIImageView imageNamed:#"Fish.png"];
fish.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"swim01.png"],
[UIImage imageNamed:#"swim02.png"],
[UIImage imageNamed:#"swim03.png"],
[UIImage imageNamed:#"swim04.png"],
[UIImage imageNamed:#"swim05.png"],
[UIImage imageNamed:#"swim06.png"],
[UIImage imageNamed:#"swim05.png"],
[UIImage imageNamed:#"swim04.png"],
[UIImage imageNamed:#"swim03.png"],
[UIImage imageNamed:#"swim02.png"], nil];
fish.animationDuration = 1.50;
fish.animationRepeatCount = -1;
[fish startAnimating];
[self.view addSubview:fish];
fish.bounds = CGRectMake(0, 0, 56, 56);
fish.position = CGPointMake(self.view.bounds.size.height / 2,
self.view.bounds.size.width / 2);
Related
I am trying to overlap two local images and trying to show the overlapped one in third image.
I am using this code but simulator shows nothing.
- (void)viewDidLoad
{
[super viewDidLoad];
image1 = [[UIImage alloc]init];
image1 = [UIImage imageNamed:#"iphone.png"];
imageA = [[UIImageView alloc]initWithImage:image1];
[self merge];
}
-(void)merge
{
CGSize size = CGSizeMake(320, 480);
UIGraphicsBeginImageContext(size);
CGPoint thumbPoint = CGPointMake(0,0);
imageview.image = imageA.image;
[imageA.image drawAtPoint:thumbPoint];
imageB = [[UIImage alloc]init];
imageB = [UIImage imageNamed:#"Favorites.png"];
CGPoint starredPoint = CGPointMake(0, 0);
[imageB drawAtPoint:starredPoint];
UIImage *imageC = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
imageview.image = imageC;
[self.view addSubview:imageview];
}
I can't figure out/don't know where i am making mistake.
Any help would be appreciable.
Remove all the code from every where except the below code in Merge.
-(void)merge
{
CGSize size = CGSizeMake(320, 480);
UIGraphicsBeginImageContext(size);
CGPoint point1 = CGPointMake(0,0);
// The second point has to be some where different than the first point, other wise, the second image will be above the first image, and you wont even know that the two images are there.
CGPoint point2 = CGPointMake(100,100);
UIImage *imageOne = [UIImage imageNamed:#"Image1.png"];
[imageOne drawAtPoint:point1];
UIImage *imageTwo = [UIImage imageNamed:#"Image2.png"];
// If you want the above image to have some blending, then you can do some thing like below.
// [imageTwo drawAtPoint:point2 blendMode:kCGBlendModeMultiply alpha:0.5];
[imageTwo drawAtPoint:point2];
UIImage *imageC = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(100,100,200,200)];
iv.image=imageC;
[self.view addSubview:iv];
}
here's a general purpose "merge" function wrote as a UIImage category... allows image overlay/underlay.
http://saveme-dot-txt.blogspot.com/2011/06/merge-image-function.html
I am creating an app that send photo on email,i have created gallery,now i want my app can write text on that photo and send it on email. Any suggestions? tutorials are acceptable.
Simplest but tricky, Add a UIView inside a UIImageView and a UILabel, update with yourimage and text. Now take an UIImage by capture screenshot here. and attach it with your email.
You can create UIImageView with image then place UILabel with clear background on image view, and finally make a UIImage using Core Graphics.
Something like this:
UIImage *bcg = [UIImage imageNamed:#"image.png"];
UIImageView *imgView = [[UIImageView alloc] initWithImage:bcg];
imgView.frame = CGRectZero;//Some frame
[self addSubview:imgView];
[imgView release];
UILabel *text = [[UILabel alloc] initWithFrame:CGRectMake(0, imgView.frame.size.height - 20, imgView.frame.size.width, 20)];
text.backgroundColor = [UIColor clearColor];
text.text = #"Some text";
[imgView addSubview:text];
UIImage *resultImage = nil;
UIGraphicsBeginImageContext(CGSizeMake(self.bounds.size.width,
self.bounds.size.height));
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGSize imageSize = CGSizeMake(self.bounds.size.width,
self.bounds.size.height);
UIGraphicsBeginImageContext(imageSize);
[viewImage drawInRect:CGRectMake(0,
0,
imageSize.width,
imageSize.height)
blendMode:kCGBlendModeNormal alpha:1];
resultImage = UIGraphicsGetImageFromCurrentImageContext();
This method easier to debug than draw image and text in context.
I have simple View based application. I had taken only UILabel on it.
Following is my code in viewDidLoad:
lblBack.textColor = [UIColor blueColor];
UIImage *img = [UIImage imageNamed:#"cn3.png"];
lblBack.backgroundColor = [UIColor colorWithPatternImage:img];
lblBack.text = #"Hello World!!!...";
// UIImageView *imgView = [[UIImageView alloc] initWithImage:img];
//
// CGRect rect = CGRectMake(0, 0, lblBack.frame.size.width, lblBack.frame.size.height);
// [imgView setFrame:rect];
// NSLog(#"Rect : %#",NSStringFromCGRect(rect));
// [img drawInRect:rect];
// imgView.contentMode = UIViewContentModeScaleAspectFill;
// imgView.contentMode = UIViewContentModeScaleAspectFit;
// imgView.contentMode = UIViewContentModeScaleToFill;
// [lblBack addSubview:imgView];
Comments shows some of the things that i have tried. I am getting following output:
In this one image is repeated 3 times. But I want that image should be stretched to fill the Label width.
I have referred some of the previous links that shows me to add Image in background and use clearColor for UILabel. Also seen example of Adding custom view in Background. But all this I dont want to use unless I dont have other solutions...
I just want to perform all things on UILabel only.... no other control except UIImage or UIImageView i want to use..
Dhiren try this code :
UIImage *img = [UIImage imageNamed:#"cab.png"];
CGSize imgSize = testLabel.frame.size;
UIGraphicsBeginImageContext( imgSize );
[img drawInRect:CGRectMake(0,0,imgSize.width,imgSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
testLabel.backgroundColor = [UIColor colorWithPatternImage:newImage];
I have tested this code.
Porting over Maulik's answer to Swift 2.2, you get the following code:
var img: UIImage = UIImage(named: "cabImage")!
var imgSize: CGSize = testLabel.frame.size
UIGraphicsBeginImageContext(imgSize)
img.drawInRect(CGRectMake(0, 0, imgSize.width, imgSize.height))
var newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext();
testLabel.backgroundColor = UIColor(patternImage: newImage)
Personally tested to be working!
I'm creating a CALayer like this:
UIColor *color = [UIColor colorWithPatternImage:[UIImage imageNamed:#"MyPattern.png"]];
backgroundLayer = [[CALayer alloc] init];
[backgroundLayer setBackgroundColor:[color CGColor]];
[[self layer] addSublayer:backgroundLayer];
For some reason, the pattern is drawn upside down. I've already tried setting geometryFlipped and applying sublayer transforms, but it doesn't work.
Using [backgroundLayer setTransform:CATransform3DMakeScale(1.0, -1.0, 1.0)]; actually does work.
I would rather use this method:
CALayer *backgroundLayer = [CALayer layer];
backgroundLayer.frame = CGRectMake(50, 50, 200, 200);
UIImage *img = [UIImage imageNamed:#"MyPattern.png"];
CGImageRef imgRef = CGImageRetain(img.CGImage);
backgroundLayer.contents = (id) imgRef;
[self.layer addSublayer:backgroundLayer];
This will provide image content inside the layer and it fits automatically, so it works great for backgrounds. Im not sure why your method isnt working, sorry about that, but I thought it would be nice to provide you with this method.
Instead of using CATransform3DMakeScale you can flip image yourself:
UIImage* img = [UIImage imageNamed:#"MyPattern.png"];
img = [UIImage imageWithCGImage:img.CGImage
scale:img.scale
orientation:UIImageOrientationDownMirrored];
UIColor* color = [UIColor colorWithPatternImage:img];
…
This works fine for me.
Or you can go further and actually redraw the image.
- (UIImage*)flipImage:(UIImage*)image {
UIGraphicsBeginImageContext(image.size);
CGContextDrawImage(UIGraphicsGetCurrentContext(),CGRectMake(0.,0., image.size.width, image.size.height),image.CGImage);
UIImage* img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
Details why it can be useful are in this answer.
I'm using UIImageView to run a flipbook anim like this:
mIntroAnimFrame = [[UIImageView alloc] initWithFrame:CGRectMake( 0, 0, 480, 320);
mIntroAnimFrame.image = [UIImage imageNamed:#"frame0000.tif"];
Basically, when determine it is time, I flip the image by just calling:
mIntroAnimFrame.image = [UIImage imageNamed:#"frame0000.tif"];
again with the right frame. Should I be doing this differently? Are repeated calls to set the image this way possibly bogging down main memory, or does each call essentially free the previous UIImage because nothing else references it? I suspect the latter is true.
Also, is there an easy way to preload the images? The anim seems to slow down at times. Should I simply load all the images into a dummy UIImage array so they are preloaded, then refer to it when I want to show it with mIntroAnimFrame.image = mPreloadedArray[i]; ?
I was typing up an example but remembered there was a perfect one at http://www.iphoneexamples.com/
NSArray *myImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"myImage1.png"],
[UIImage imageNamed:#"myImage2.png"],
[UIImage imageNamed:#"myImage3.png"],
[UIImage imageNamed:#"myImage4.gif"],
nil];
UIImageView *myAnimatedView = [UIImageView alloc];
[myAnimatedView initWithFrame:[self bounds]];
myAnimatedView.animationImages = myImages;
myAnimatedView.animationDuration = 0.25; // seconds
myAnimatedView.animationRepeatCount = 0; // 0 = loops forever
[myAnimatedView startAnimating];
[self addSubview:myAnimatedView];
[myAnimatedView release];
Was this what you were thinking of?
[UIImage imageNamed:] will cache images, so they will not be immediately freed. There is nothing wrong with that, but if you know you will not use the image again for a while, use a different method to get the images.
UIImageView also has built in animation abilities if you have a regular frame rate.
mIntroAnimFrame.animationImages = mPreloadedArray;
[mIntroAnimFrame startAnimating];