iOS: opening image from photolibrary - ios

I'm calling a view (that contains just an ImageView) passing the picture's path from library's user. And then, on viewdidload I'm loading the image. But, the image is getting a "super zoom" on the upper right corner of the picture, I don't know why!
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationController.navigationBar.translucent = NO;
UIImage *picture = [UIImage imageWithContentsOfFile: self.picturePath];
UIImageView* imageView = [[UIImageView alloc] initWithImage:picture];
imageView.contentMode = UIViewContentModeScaleAspectFit;
[self.view addSubview:imageView];
}

Based on your comment, the image you are loading is very large. The UIImageView that you're creating is taking on the width/height of the image. You can either set the frame of imageView, or use and IBOutlet from your nib to add the photo to an existing UIImageView.
In your code you're creating a new UIImageView, so your updated code would look like:
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationController.navigationBar.translucent = NO;
UIImage *picture = [UIImage imageWithContentsOfFile: self.picturePath];
UIImageView* imageView = [[UIImageView alloc] initWithImage:picture];
// Set your frame size
imageView.frame = CGRectMake(0.0, 0.0, 320.0, 320.0);
imageView.contentMode = UIViewContentModeScaleAspectFit;
[self.view addSubview:imageView];
}

Related

Bug about UIImageView display gif?

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];

How to set a small image on a uiimage view on a particular position?

I created an UIView.
I need to set a small image on a particular position.
I can do it setting the image in a small separate view.
But, I plan to do it dynamically on a Large UIView which is full screen.
Thanks.
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,200)];
view.backgroundColor=[UIColor clearColor];
[self.view addSubview:view];
// add image in view
UIImageView *imageView =[[UIImageView alloc] initWithFrame:CGRectMake(50,50,20,20)];
imageView.image=[UIImage imageNamed:#"image.png"];
[view addSubview:imageView];
That is what views are for: manipulating UI widgets.
By far, your best approach is to position a UIImageView.
You can also override the view drawing methods:
override func drawRect(rect: CGRect) {
// Curstom
}
Try this code.
UIImage *image = [UIImage imageNamed:#"imagename.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
//specify the frame of the imageView in the superview , here it will fill the superview
imageView.frame = catView.bounds;
// set custom frame as per your requirement
//or
// imageView.frame = CGRectMake(10,10,30,30);
//or
//according to superviews frame
//imageView.frame=CGRectMake(10,10,yourUIViewObject.bounds.size.width/2,yourUIViewObject.bounds.size.height/2);
// add the imageview to the superview
[yourUIViewObject addSubview:imageView];

How do I load image to a view?

I'm very new to ios. I'm trying to load a image to my view. Here is my code:
-(void)loadView
{
UIImageView *imageHolder = [[UIImageView alloc] initWithFrame:CGRectMake(40, 40, 40, 40)];
UIImage *image = [UIImage imageNamed:#"test.jpg"];
imageHolder.image = image;
[imageHolder sizeToFit];
[self.view addSubview:imageHolder];
}
in my class named Test which is a sub class of UIViewController. test.jpg is in the same directory. When I run, I'm getting an error at this line:
UIImage *image = [UIImage imageNamed:#"test.jpg"];
Thread1:EXC_BAD_ACCESS(code=2,address=0xbasd)
where I'm making mistake?
I'm using ios7 and xcode 5
I see the problem in your code. It cause infinity loop.
loadView - this method is called when the view is empty and you are responsible of creating setting view.
When you access self.view your view is empty and loadView method will be called again, and it will finish in infinite loop.
UIImageView *imageHolder = [[UIImageView alloc] initWithFrame:CGRectMake(40, 40, 40, 40)];
UIImage *image = [UIImage imageNamed:#"test.jpg"];
imageHolder.image = image;
[imageHolder sizeToFit];
self.view = [[UIView alloc] initWithFrame:frame];
[self.view addSubview:imageHolder];
It's not important where image is located in you file structure. When you add a image to the project.
You can do it by - drag and gripping image to the project. or by - "Add Files to Project"
When you add image you should select a target project you are adding it to.
Instead of using a loadView method, I think you should put your code inside -(void)viewDidLoad method. By the way, in Xcode5, you need to right-click on your project and "Add files to project" to add this image file.
-(void)viewDidLoad
{
[super viewDidLoad];
UIImageView *imageHolder = [[UIImageView alloc] initWithFrame:CGRectMake(40, 40, 40, 40)];
UIImage *image = [UIImage imageNamed:#"test.jpg"];
imageHolder.image = image;
[imageHolder sizeToFit];
[self.view addSubview:imageHolder];
}

Centering a UIImageView inside a UIScrollView

I am trying to center a UIImageView in a scroll view. Here is the code I tried with no results.
UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:kReceiptDetailScrollViewBackgroundCornered]];
[iv setContentMode:UIViewContentModeCenter];
[[self scrollView] insertSubview:iv atIndex:0];
Anybody know what's wrong with this?
You set the image view's content mode to center, which means that the image is not scaled and it can be larger than the image view that displays it.
UIImage *image = [UIImage imageNamed: kReceiptDetailScrollViewBackgroundCornered];
UIImageView *iv = [[UIImageView alloc] initWithImage: image];
iv.contentMode = UIViewContentModeScaleAspectFit;
iv.frame = [self scrollView].bounds;
[[self scrollView] addSubview: iv];

Add UIImageView as Subview

I'm trying to create an animated UIImageView with certain image constraints.
I want the UIImageView animating at a size of 141x262, and not the entire screen. Currently I have this code:
CJ = [[UIImageView alloc] initWithFrame:self.view.frame];
CJ.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"CJ_1.png"],
[UIImage imageNamed:#"CJ_2.png"],
[UIImage imageNamed:#"CJ_3.png"], nil];
CJ.animationDuration = 1;
CJ.animationRepeatCount = 0;
[CJ startAnimating];
[self.view addSubview:CJ];
Any and all help will be appreciated!
If you want the view to be a different size, set its frame to be that size:
CJ = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 141, 162)];
If your images are large, you probably also want to set a contentMode on your view. For example:
CJ.contentMode = UIViewContentModeScaleAspectFit;

Resources