Bug about UIImageView display gif? - ios

I create a UIImageView and add to VC UIView , then I create a image with animatedImageWithImages:duration:
When I execute the code, it works normally. It displays image animation. But when I push to next VC and in the process of swipe back , I find the UIImageView display nothing.
At the moment finger left screen , everything back to normal.
Here is my code:
- (void)viewDidLoad {
[super viewDidLoad];
UIImageView *imgView = [[UIImageView alloc] init];
imgView.backgroundColor = [UIColor orangeColor];
imgView.frame = CGRectMake(20, 200, 80, 80);
[self.view addSubview:imgView];
NSArray *imgs = #[[UIImage imageNamed:#"img1"],[UIImage imageNamed:#"img2"]];
imgView.image = [UIImage animatedImageWithImages:imgs duration:0.5];
}
BugGif
I notice the imageView has CAKeyframeAnimation. I guess the conflict that runmode and CAKeyframeAnimation.
SDWebImage has the same problem. Because it also use the method that create UIImage. I think the point is the method of animatedImageWithImages:duration: and CAKeyframeAnimation.
Anyone knows the solution? Thanks a lot.
The event that the finger left the screen will make imageView display normally. Why and How to resolve?
GitHubBugDemo

You can use UIImageView another methods to set images and animation duration as below
UIImageView *imgView = [[UIImageView alloc] init];
imgView.backgroundColor = [UIColor orangeColor];
imgView.frame = CGRectMake(20, 200, 80, 80);
NSArray *imgs = #[[UIImage imageNamed:#"img1"],[UIImage imageNamed:#"img2"]];
[imgView setAnimationImages:imgs];
[imgView setAnimationDuration:1];
[imgView setAnimationRepeatCount:HUGE_VAL];
[imgView startAnimating];
[self.view addSubview:imgView];

Related

Adding long press gesture to UIImageView inside Scrollview

I've been researching this for a couple of hours now and nothing seems to work. I have a UIScrollView as an IBOutlet and am adding two UIImageViews via code. One is a background image and the 2nd is a photo. Everything works fine here. I am trying to add a long press gesture to the second photo but it doesn't seem to be working. Here is my code. I hope someone can see my problem.
// loop thru images array
UIImage *bgFrame = [UIImage imageNamed:#"photo-frame.png"];
UIImageView *bgImageView = [[UIImageView alloc] initWithImage:bgFrame];
bgImageView.frame = CGRectMake(posX, posY, 100, 100);
[self.scroll addSubview:bgImageView];
NSString *photoName = [self.photoArray objectAtIndex:i];
UIImage *photo = [self.utils getClientPhoto:photoName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:photo];
imageView.userInteractionEnabled = YES;
imageView.frame = CGRectMake(6, 9, 89, 71);
imageView.tag = i;
[bgImageView addSubview:imageView];
// add long press for deletion
UILongPressGestureRecognizer *lPressed = [[UILongPressGestureRecognizer alloc]
initWithTarget:self action:#selector(imageLPressed:)];
lPressed.delegate = self;
lPressed.minimumPressDuration = 0.4;
[imageView addGestureRecognizer:lPressed];
imageView.userInteractionEnabled = YES; not needed, it always work fine. Setting delegate to gestureRecognizer is optional too. First of all try to add imageView to self.scroll subviews not to bgImageView. After that check your scrollView content size, if your image is out of dat size it can be visible but not available. Try to replace imageview with uibutton and check for it's uitouchupinside event.
Enable UserInteraction on bgImageView andimageView.

How to display image in iphone simulator using objective C

I am trying to display image in iphone simulator programmatically using objective c, I am using following code, but it is not displaying anything in simulator
imageview = [[UIImageView alloc] init];
UIImage *myimg = [UIImage imageNamed:#"A1.jpg"];
imageview.image=myimg;
You can use this code
imageview = [[UIImageView alloc] init];
UIImage *myimg = [UIImage imageNamed:#"A1.jpg"];
imageview.image=myimg;
imageview.frame = CGRectMake(50, 50, 150, 150); // pass your frame here
[self.view addSubview:imageview];
Maybe you forgot this:
imageView.frame = self.view.bounds;
[self.view addSubview:imageview];
If you are programmatically creating the imageView then you must add it to the view heirarchy using below line
[self.view addSubview:imageview];
Add your image view into view like this
imageview = [[UIImageView alloc] initWithframe:CGRectMake(0, 0, 100, 200)];
UIImage *myimg = [UIImage imageNamed:#"A1.jpg"];
imageview.image=myimg;
[self.view addSubview:imageview];
First you need to initialize your UIImageView and assign UIImage to it's image property. You already did it in your code. Then you can set the frame to image view.
imageview.frame = CGRectMake(50, 50, 150, 150);
But in most cases it will be more useful to pass sizeToFit message to your image view.
[imageview sizeToFit];
So it will change it's frame to fit image's size. But using this approach you need to assign the origin to your image view's frame later. So the best solution is assign the frame in initialization and then pass sizeToFit.
UIImageView *imageview = [[UIImageView alloc] initWithFrame:CGRectMake(10.0, 20.0, 0.0, 0.0)];
UIImage *myimg = [UIImage imageNamed:#"A1.jpg"];
imageview.image=myimg;
[imageview sizeToFit];
And don't forget to add image view to view hierarchy.
[self.view addSubview:imageview];
Also you need to check if your image is correctly added to your project. You can check it by finding you image file in Project Navigator and checking it's Target Membership in File Inspector. There should be a tick beside you target's name.

How to set the image in Background image in UIViewController in ios

How to set the image in Background image(means how to paste the image in Background image). I took one image as a background image in UIViewController. Right now i want to put the another image in background image.i know how to put the image as a Background image in UIViewController.But i have no idea how to put the image in background image.I tried the code.But the image will not displayed in background image.This is my code.please help me any body.Thanks in advance.
-(void)viewDidLoad
{
backgroundImage=[UIImage imageNamed:#"bg-small.PNG"];
audioViewBackgroundImage=[[UIImageView alloc]initWithImage:backgroundImage];
[self.view addSubview:audioViewBackgroundImage];
mImage=[UIImage imageNamed:#"muluguphoto.png"];
mBackgroundImage=[[UIImageView alloc]initWithFrame:CGRectMake(20, 160, 150, 90)];
mBackgroundImage=[[UIImageView alloc]initWithImage:mImage];
[audioViewBackgroundImage addSubview:mBackgroundImage];
}
add it to your viewDidLoad..
UIImageView *newImage=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[newImage setBackgroundColor:[UIColor redColor]];//here else you can add image
[self.view addSubview:newImage];
UITableView *newTable=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[newTable setBackgroundColor:[UIColor clearColor]];
[self.view addSubview:newTable];
you have to set imageview above imageview in userinterface.like in photo.make proper heirARCHY!and set frame of imageview INDependentaly as you want.here is background image with logo image..
Add this line to viewDidLoad
self.view.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:#"background.png"]];
Why do you initializing mBackgroundImage two times?
Let's try the below code and let me know the result.
UIImageView * audioViewBackgroundImage=[[UIImageView alloc] initWithFrame:self.view.frame];
audioViewBackgroundImage.image = [UIImage imageNamed:#"bg-small.PNG"];
[self.view addSubview:audioViewBackgroundImage];
UIImageView * mBackgroundImage =[[UIImageView alloc]initWithFrame:CGRectMake(20, 160, 150, 90)];
mBackgroundImage.image = [UIImage imageNamed:#"muluguphoto.png"];
[audioViewBackgroundImage addSubview:mBackgroundImage];
Thanks!
UIImageView * bgImage =[[UIImageView alloc]initWithFrame:self.view.frame];
bgImage.image = [UIImage imageNamed:#"myimg.png"];
[self.view addSubview:bgImage];
[self.view sendSubviewToBack:bgImage];
//write this line in viewdidload and make sure that you have import the image in your project by copying it .
when you write this line of code then this image will automatically replace the previous background image
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"stack.jpeg"]];

UIView Animation does not actually set new values?

I'm having a problem with UI animations.
I have a uiview: theView.
I animate theView to move to some random position on the mainView. It moves from point A to point B and changes its scale from 1.0 to 1.5;
So after the animation, the view is bigger and in a new position.
I call a second animation, by button. This one only move theView.
After the animation, the view has moved. However, the view is no longer bigger, its scale is back at 1.0.
So do these animation values not become theViews new values for size,position,ex?
I will post code soon, because it of course may be a careless error I am missing.
If its UIView animation.
- (void)viewDidLoad
{
[super viewDidLoad];
_yourView = [[UIView alloc]initWithFrame:CGRectMake(300, 300, 100, 100)];
[_yourView setBackgroundColor:[UIColor redColor]];
[self.view addSubview:_yourView];
[UIView animateWithDuration:1.0f animations:^{
[_yourView setFrame:CGRectMake(400, 400, 150, 150)];
} completion:nil];
UIButton *aMoveButton = [[UIButton alloc] initWithFrame:CGRectMake(10.0f, 5.0f, 60.0f, 40.0f)];
[aMoveButton setTitle:#"MoveView" forState:UIControlStateNormal];
[aMoveButton addTarget:self action:#selector(moveYourView) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:aMoveButton];
}
-(void)moveYourView{
[UIView animateWithDuration:1.0f animations:^{
[_yourView setFrame:CGRectMake(300, 500, 150, 150)];
} completion:nil];
}
Believe me this works and it actually changes the view's values for size,position.
I cannot post a comment, so I would just like to suggest that you say UIView, i'm not 100% sure, so dont take my word for it, but im pretty sure you should use UIImageView instead. Hope that helps a little :)
// create the view that will execute our animation
UIImageView* campFireView = [[UIImageView alloc] initWithFrame:self.view.frame];
// load all the frames of our animation
campFireView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"campFire01.gif"],
[UIImage imageNamed:#"campFire02.gif"],
[UIImage imageNamed:#"campFire03.gif"],
[UIImage imageNamed:#"campFire04.gif"],
[UIImage imageNamed:#"campFire05.gif"],
[UIImage imageNamed:#"campFire06.gif"],
[UIImage imageNamed:#"campFire07.gif"],
[UIImage imageNamed:#"campFire08.gif"],
[UIImage imageNamed:#"campFire09.gif"],
[UIImage imageNamed:#"campFire10.gif"],
[UIImage imageNamed:#"campFire11.gif"],
[UIImage imageNamed:#"campFire12.gif"],
[UIImage imageNamed:#"campFire13.gif"],
[UIImage imageNamed:#"campFire14.gif"],
[UIImage imageNamed:#"campFire15.gif"],
[UIImage imageNamed:#"campFire16.gif"],
[UIImage imageNamed:#"campFire17.gif"], nil];
// all frames will execute in 1.75 seconds
campFireView.animationDuration = 1.75;
// repeat the annimation forever
campFireView.animationRepeatCount = 0;
// start animating
[campFireView startAnimating];
// add the animation view to the main window
[self.view addSubview:campFireView];

UIImageView shows outside its parents frame

I'm developing a iOs app for iPad and I'm implementing a UIImageView inside a UIView. The problem is that if the position of the image view has a position out of the UIView that it is inside, you can see it. The code:
UIView *vistafunda = [[UIView alloc] initWithFrame:CGRectMake(512/2, 48, 525, 651)];
[self.view addSubview:vistafunda];
vistafunda.backgroundColor = [UIColor darkGrayColor];
UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"hola.png"]];
img.center = CGPointMake(12, 200);
[vistafunda addSubview:img];
I would like that the image view was always under the view, so if the image is outside the view you could not see it.
Thanks!
Just make the containing view clip it's children
vistafunda.clipsToBounds = YES;

Resources