Image crop stretching image from camera - ios

I'm using this project: http://code4app.net/ios/Perfect-Image-Cropper/51de11576803fa4548000004 for cropping images.
The fact is that is working perfect with photos imported from Library, but not with image from camera.
When selecting image from camera it's showing cropped but stretched.
#interface ImageViewController (){
ImageCropperView *cropper;
UIView *containerView;
UIImageView *finImageView;
UIButton *removeBtn;
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *mediaType = info[UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
// Media is an image
UIImage *image = info[UIImagePickerControllerOriginalImage];
if (image != nil) {
[self resetCropper];
containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 568)];
[self.view addSubview:containerView];
cropper = [[ImageCropperView alloc] initWithFrame:CGRectMake(10, 10, 300, 150)]
[cropper setup];
cropper.image = image;
// Adding the crop button
UIButton *cropBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[cropBtn addTarget:self action:#selector(cropImage:) forControlEvents:UIControlEventTouchUpInside];
[cropBtn setTitle:#"Select" forState:UIControlStateNormal];
cropBtn.frame = CGRectMake(140, 160, 80, 30.0);
[containerView addSubview:cropBtn];
[containerView addSubview:cropper];
}
}
}
// Action when crop button clicked
- (IBAction)cropImage:(id)sender
{
[cropper finishCropping];
UIImage *newImage = cropper.croppedImage;
finImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 300, 150)];
finImageView.image = newImage;
[cropper removeFromSuperview];
[containerView removeFromSuperview];
[self.scrollView insertSubview:finImageView belowSubview:self.actionsTab];
// Adding a button for remove
removeBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
removeBtn.frame = CGRectMake( 0, 0, 22, 22);
[removeBtn setBackgroundImage:[UIImage imageNamed:#"remove"] forState:UIControlStateNormal];
[removeBtn addTarget:self action:#selector(removePhoto:) forControlEvents:UIControlEventTouchUpInside];
[finImageView insertSubview:removeBtn aboveSubview:finImageView];
finImageView.userInteractionEnabled = YES;
}
- (IBAction)removePhoto:(id)sender{
[self resetCropper];
[finImageView removeFromSuperview];
}
- (void)resetCropper
{
[cropper reset];
finImageView.image = nil;
[removeBtn removeFromSuperview];
}
Any help will be very appreciate!

Related

UIButton not changing anything with #selector - new Issue

Need help. I am trying to change an image of an ImageView using a programmed button. I copied some of the sample codes out here. The sitting on highlighted button works but not doing anything else when clicked. Thanks.
Thanks for the replies. I managed to change the photos but created more problem. I believe it is clear that that I am new to Xcode.
New problems:
1. The new image stays on top so i cannot access the button.
2. I would like to have the image changed only when the button is highlighted.
3. With the new image, it created warning: "Hides instance variable" on the relayImages properties. Thanks again.
.h file
-(void)changeLabel:(UIButton *)sender;
-(void)addMyButton;
-(void)addMyImages;
-(void)addMyLabel;
#property (nonatomic) UIButton *playButton;
#property (nonatomic) UIImageView *relayImages;
#property (nonatomic, strong) UILabel *checkStatusLabel;
#synthesize relayImages;
.m file
- (void)viewDidLoad
{
[super viewDidLoad];
[self addMyImages];
[self addMyButton];
[self addMyLabel];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)addMyButton
{
UIButton *playButton = [[UIButton alloc] initWithFrame:CGRectMake(100, 348, 20, 78)];
UIImage *offState = [UIImage imageNamed:#"PBNoOff.png"];
[playButton setBackgroundImage:offState forState:UIControlStateNormal];
UIImage *onState = [UIImage imageNamed:#"PBNoOn.png"];
[playButton setBackgroundImage:onState forState:UIControlStateHighlighted];
[playButton addTarget:self action:#selector(changeLabel:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:playButton];
}
-(void)changeLabel:(UIButton *)sender
{
UIImage *image1 = [UIImage imageNamed:#"1_2RelaysOn.png"];
UIImageView *relayImages = [[UIImageView alloc] initWithImage:image1];
relayImages.frame = CGRectMake(0, 0, 320, 480);
relayImages.backgroundColor = [UIColor whiteColor];
[self.view addSubview:relayImages];
[self.view addSubview:_playButton];
}
-(void)addMyImages
{
UIImage *image = [UIImage imageNamed:#"1_2RelaysOff.png"];
UIImageView *relayImages = [[UIImageView alloc] initWithImage:image];
relayImages.frame = CGRectMake(0, 0, 320, 480);
relayImages.backgroundColor = [UIColor whiteColor];
[self.view addSubview:relayImages];
}
-(void)addMyLabel
{
UILabel *checkStatusLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 50, 200, 30)];
checkStatusLabel.backgroundColor = [UIColor greenColor];
checkStatusLabel.textColor = [UIColor whiteColor];
[self.view addSubview:checkStatusLabel];
}
try this i am created custom imageView
.m file
-(void)addMyButton
{
_playButton = [[UIButton alloc] initWithFrame:CGRectMake(100, 348, 60, 50)];
[_playButton setTitle:#"button" forState:UIControlStateNormal];
[_playButton setBackgroundColor:[UIColor grayColor]];
UIImage *offState = [UIImage imageNamed:#"1.jpg"];
// [playButton setBackgroundImage:offState forState:UIControlStateNormal];
// UIImage *onState = [UIImage imageNamed:#"PBNoOn.png"];
[_playButton setBackgroundImage:offState forState:UIControlStateHighlighted];
[_playButton addTarget:self action:#selector(changeLabel:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_playButton];
}
-(void)changeLabel:(UIButton *)sender
{
UIImage *image1 = [UIImage imageNamed:#"2.jpg"];
relayImages = [[UIImageView alloc] initWithImage:image1];
relayImages.frame = CGRectMake(20, 100, 100, 100);
relayImages.backgroundColor = [UIColor whiteColor];
[self.view addSubview:relayImages];
}
-(void)addMyImages
{
UIImage *image = [UIImage imageNamed:#"3.jpg"];
UIImageView *someImages = [[UIImageView alloc] initWithImage:image];
someImages.frame = CGRectMake(20, 200, 100, 100);
someImages.backgroundColor = [UIColor whiteColor];
[self.view addSubview:someImages];
}
-(void)addMyLabel
{
_checkStatusLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 250, 100, 30)];
_checkStatusLabel.backgroundColor = [UIColor greenColor];
_checkStatusLabel.textColor = [UIColor whiteColor];
[self.view addSubview:_checkStatusLabel];
}
.h file
-(void)changeLabel:(UIButton *)sender;
-(void)addMyButton;
-(void)addMyImages;
-(void)addMyLabel;
#property (nonatomic) UIButton *playButton;
#property (nonatomic) UIImageView *relayImages;
#property (nonatomic, strong) UILabel *checkStatusLabel;
and viewdidload
[self addMyButton];
[self addMyImages];
[self addMyLabel];

how to change uiimageview image's programmatically

In my ios application i create uiimageviews programmatically like that,
for (int i=0; i<20; i++) {
productID=[products objectAtIndex:i];
UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(column*197+38 , row*350+8, 159, 45)];
//[button setImage:redButtonImage forState:UIControlStateNormal];
[button addTarget:self
action:#selector(buttonAction:)
forControlEvents:UIControlEventTouchUpInside];
button.tag = productID;
[scrollView addSubview:button];
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(column*197+170, row*350+25, 13, 13)];
imageView.tag=productID;
UIImage *image = [UIImage imageNamed:#"redImage.png"];
[imageView setImage:image];
[scrollView addSubview:imageView];
}
in my buttonActon, i want to change that uiimageview's image to another image. But i can not do that. Can anyone help me? Thank You.
try like this ,
for (int i=0; i<20; i++) {
productID=[products objectAtIndex:i];
UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(column*197+38 , row*350+8, 159, 45)];
//[button setImage:redButtonImage forState:UIControlStateNormal];
[button addTarget:self
action:#selector(buttonAction:)
forControlEvents:UIControlEventTouchUpInside];
button.tag = 100+productID;
[scrollView addSubview:button];
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(column*197+170, row*350+25, 13, 13)];
imageView.tag=productID;
UIImage *image = [UIImage imageNamed:#"redImage.png"];
[imageView setImage:image];
[scrollView addSubview:imageView];
}
keep this one in button action method and pass the imageview tag in the place of imgview.tag
UIImageView *img=(UIImageView *)[scrollView viewWithTag:imgview.tag];
img.image=[UIImage imageNamed:#"name.png"];
Instead of this:
imageView.tag=UrunId;
Write:
imageView.tag = productID + 100;
Because you need a unique tag for each Image View.
Then implement the buttonAction: like:
- (void)buttonAction:(UIButton *)sender
{
UIImageView *imageView = (UIImageView *)[scrollView viewWithTag:(sender.tag+100)];
[imageView setImage:yourImage];
}
Please try to use this one
You should have different tag for button and image.So please give first different tag like this....
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(column*197+170, row*350+25, 13, 13)];
imageView.tag=button.tag+1;
UIImage *image = [UIImage imageNamed:#"redImage.png"];
[imageView setImage:image];
[scrollView addSubview:imageView];
After that do this...
- (void)buttonAction:(UIButton *)sender
{
UIImageView *imageView = (UIImageView *)[scrollView viewWithTag:sender.tag+1];
imageView.image=[UIImage imageNamed:#"imageName"];
}

addSubview crashes iOS 5.1 but not 6.1

The following works fine in 6.1 but the app crashes in 5.1...
weeklyHeaderViewController =[[WeeklyHeaderViewController alloc] init];
[self.view addSubview:weeklyHeaderViewController.view];
This is an iPad app and the view is 939x31 .xib containing 7 UILabel pairs. The controller'r viewDidLoad method inserts a 4x30 image seperator between the 7 pairs then moves the view into position at the bottom of the screen.
I can trace it through the initWithNibName's so it appears to be alloc ining fine. It crashes with at the addSubview and never gets to the viewDidLoad method.
Is there is something I should here that I am missing with regard to 5.1?
Any suggestions would be appreciated.
Thanks,
John
// WeeklyHeaderViewController.m
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
CGRect myImageRect = CGRectMake(131, 0, 4, 30);
UIImageView *image = [[UIImageView alloc] initWithFrame:myImageRect];
[image setImage:[UIImage imageNamed:#"weeklyDaySeparator.png"]];
[self.view addSubview:image];
[image release];
myImageRect = CGRectMake(266, 0, 4, 30);
image = [[UIImageView alloc] initWithFrame:myImageRect];
[image setImage:[UIImage imageNamed:#"weeklyDaySeparator.png"]];
self.view addSubview:image];
[image release];
myImageRect = CGRectMake(400, 0, 4, 30);
image = [[UIImageView alloc] initWithFrame:myImageRect];
[image setImage:[UIImage imageNamed:#"weeklyDaySeparator.png"]];
[self.view addSubview:image];
[image release];
myImageRect = CGRectMake(535, 0, 4, 30);
image = [[UIImageView alloc] initWithFrame:myImageRect];
[image setImage:[UIImage imageNamed:#"weeklyDaySeparator.png"]];
[self.view addSubview:image];
[image release];
myImageRect = CGRectMake(670, 0, 4, 30);
image = [[UIImageView alloc] initWithFrame:myImageRect];
[image setImage:[UIImage imageNamed:#"weeklyDaySeparator.png"]];
[self.view addSubview:image];
[image release];
myImageRect = CGRectMake(804, 0, 4, 30);
image = [[UIImageView alloc] initWithFrame:myImageRect];
[image setImage:[UIImage imageNamed:#"weeklyDaySeparator.png"]];
[self.view addSubview:image];
[image release];
self.view.frame = CGRectMake(74, 687-14, self.view.frame.size.width, self.view.frame.size.height);
}
Thanks to iOSBegginer who reminded me to uncheck AutoLayout in my xib.

Showing UIButton Done on UIScrollview for images

Want to show UIButton done on UIScrollView for images. With the below code UIButton is showing but when I scroll UIImageViews, it shows only on very first imageview where as I want UIButton done should show on all image views in UIScrollView.
- (void)viewDidLoad
{
self.view.backgroundColor = [UIColor blackColor];
UIScrollView *imageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton addTarget:self action:#selector(dismissView:) forControlEvents:UIControlEventTouchUpInside];
myButton.frame = CGRectMake(0, 0, 60, 35);
[myButton.layer setMasksToBounds:YES];
[myButton.layer setCornerRadius:10.0f];
myButton.layer.borderWidth = 2;
myButton.layer.borderColor = [[UIColor whiteColor] CGColor];
[myButton setTitle:#"Done" forState:UIControlStateNormal];
myButton.backgroundColor = [UIColor blackColor];
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithCustomView:myButton];
[[self navigationItem] setRightBarButtonItem:button animated:YES];
imageScrollView.pagingEnabled = YES;
NSInteger numberOfViews = 61;
for (int i = 0; i < numberOfViews; i++) {
CGFloat xOrigin = i * self.view.frame.size.width;
NSString *imageName = [NSString stringWithFormat:#"image%d.png", i];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height);
[imageScrollView addSubview:imageView];
[imageScrollView addSubview:myButton];
[imageView release];
}
imageScrollView.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
[self.view addSubview:imageScrollView];
[imageScrollView release];
}
How should I code it that UIButton Done should show on all image views when I scroll it.
Add your UIButton in for loop i.e. it can add each time when your new ImageView is add.
And change its x_Origin for each imageView -
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIScrollView *imageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
imageScrollView.pagingEnabled = YES;
NSInteger numberOfViews = 61;
for (int i = 0; i < numberOfViews; i++)
{
CGFloat xOrigin = i * self.view.frame.size.width;
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton addTarget:self action:#selector(dismissView) forControlEvents:UIControlEventTouchUpInside];
myButton.frame = CGRectMake(xOrigin, 0, 60, 35);
[myButton setTitle:#"Done" forState:UIControlStateNormal];
myButton.backgroundColor = [UIColor blackColor];
[myButton.layer setMasksToBounds:YES];
[myButton.layer setCornerRadius:10.0f];
myButton.layer.borderWidth = 2;
myButton.layer.borderColor = [[UIColor whiteColor] CGColor];
NSString *imageName = [NSString stringWithFormat:#"image%d.png", i];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height);
[imageScrollView addSubview:imageView];
[imageScrollView addSubview:myButton];
}
imageScrollView.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
[self.view addSubview:imageScrollView];
}
Remove these 2 lines
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithCustomView:myButton];
[[self navigationItem] setRightBarButtonItem:button animated:YES];
There is no need to do this. This means you're setting that button on rightBarButton of NavigationBar but in your case i think you dont want to do so.
Set frame for myButton in for loop similar to imageview frame.
myButton.frame= CGRectMake(xOrigin, 0, self.myButton.frame.size.width, self.myButton.frame.size.height);

iOS Subview with buttons not responding to touches.

I have a UIView Subclass that has a frame/image with buttons in it. I'm trying to add it to my view controller however when I do it shows up but none of the buttons will register ANY touches.
Here is my UIView Subclass.
#import "ControlView.h"
#implementation ControlView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
CGRect slideFrame = CGRectMake(0, 402, 320, 82);
slider = [[UIView alloc] initWithFrame:slideFrame];
slider.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"shelf.png"]];
[self addSubview:slider];
UIImage *btnImage = [UIImage imageNamed:#"NavBar_01.png"];
UIImage *btnImageSelected = [UIImage imageNamed:#"NavBar_01_s.png"];
btnImage = [UIImage imageNamed:#"NavBar_01.png"];
btnImageSelected = [UIImage imageNamed:#"NavBar_01_s.png"];
btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
btn1.frame = CGRectMake(12, 28, 70, 50);
[btn1 setBackgroundImage:btnImage forState:UIControlStateNormal];
[btn1 setBackgroundImage:btnImageSelected forState:UIControlStateSelected];
[slider addSubview:btn1];
[slider bringSubviewToFront:btn1];
[btn1 addTarget:self action:#selector(home) forControlEvents:UIControlEventTouchUpInside];
btnImage = [UIImage imageNamed:#"NavBar_02.png"];
btnImageSelected = [UIImage imageNamed:#"NavBar_02_s.png"];
btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
btn2.frame = CGRectMake(87, 28, 70, 50);
[btn2 setBackgroundImage:btnImage forState:UIControlStateNormal];
[btn2 setBackgroundImage:btnImageSelected forState:UIControlStateSelected];
[slider addSubview:btn2];
[slider bringSubviewToFront:btn2];
// [btn2 addTarget:self action:#selector() forControlEvents:UIControlEventTouchUpInside];
btnImage = [UIImage imageNamed:#"NavBar_03.png"];
btnImageSelected = [UIImage imageNamed:#"NavBar_03_s.png"];
btn3 = [UIButton buttonWithType:UIButtonTypeCustom];
btn3.frame = CGRectMake(162, 28, 70, 50);
[btn3 setBackgroundImage:btnImage forState:UIControlStateNormal];
[btn3 setBackgroundImage:btnImageSelected forState:UIControlStateSelected];
[slider addSubview:btn3];
[slider bringSubviewToFront:btn3];
// [btn3 addTarget:self action:#selector() forControlEvents:UIControlEventTouchUpInside];
btnImage = [UIImage imageNamed:#"NavBar_04.png"];
btnImageSelected = [UIImage imageNamed:#"NavBar_04_s.png"];
btn4 = [UIButton buttonWithType:UIButtonTypeCustom];
btn4.frame = CGRectMake(237, 28, 70, 50);
[btn4 setBackgroundImage:btnImage forState:UIControlStateNormal];
[btn4 setBackgroundImage:btnImageSelected forState:UIControlStateSelected];
[slider addSubview:btn4];
[slider bringSubviewToFront:btn4];
// [btn4 addTarget:self action:#selector() forControlEvents:UIControlEventTouchUpInside];
UISwipeGestureRecognizer *recognizer;
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(slideUp)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionUp)];
[self addGestureRecognizer:recognizer];
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(slideDown)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionDown)];
[self addGestureRecognizer:recognizer];
}
return self;
}
-(void)slideUp
{
// Slide up based on y axis
CGRect frame = slider.frame;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.75];
frame.origin.y = 320;
slider.frame = frame;
[UIView commitAnimations];
}
-(void)slideDown
{
CGRect frame = slider.frame;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.75];
frame.origin.y = 425;
slider.frame = frame;
[UIView commitAnimations];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
#end
And this is how I'm adding it to my ViewController.
ControlView *cont = [[ControlView alloc]init];
[homeView.view addSubview:cont];
I tried adding the same code to a ViewController and the buttons etc.. worked so I'm sure this is something simple/stupid with Delegates/Self etc.. between the subview and the ViewController however I have a mental block.
You are doing this?
ControlView *cont = [[ControlView alloc]init];
you are supposed to do
ControlView *cont = [[ControlView alloc]initWithFrame:self.view.frame];

Resources