I am new in iOS and I am facing problem to add UIscrollview on UIImage in UIAlertView.
My code is Like this
In ViewDidLoad
newsimage.userInteractionEnabled=YES;
UILongPressGestureRecognizer *longpressgestureRecognizer = [[UILongPressGestureRecognizer alloc] init];
[longpressgestureRecognizer addTarget:self action:#selector(imgLongPressed:)];
longpressgestureRecognizer.delegate = self;
[newsimage addGestureRecognizer: longpressgestureRecognizer];
- (void) imgLongPressed:(UILongPressGestureRecognizer*)sender
{
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
CGSize size = CGSizeMake(200, 200); // set the width and height
UIImage *resizedImage = [self resizeImage:newsimage.image imageSize:size];
UIImage *wonImage = resizedImage;
imageView.contentMode=UIViewContentModeCenter;
[imageView setImage:wonImage];
alertViewpress = [[UIAlertView alloc] initWithTitle:#""
message:#""
delegate:self
cancelButtonTitle:nil
otherButtonTitles:#"OK", nil];
//check if os version is 7 or above
if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
[alertViewpress setValue:imageView forKey:#"accessoryView"];
}else{
[alertViewpress addSubview:imageView];
}
[alertViewpress show];
[self performSelector:#selector(dismiss:) withObject:alertViewpress afterDelay:1.0];
}
- (void)willPresentAlertView:(UIAlertView *)alertView {
[alertView setFrame:CGRectMake(0, 0, 200, 200)];
}
-(void)dismiss:(UIAlertView*)alert
{
[alert dismissWithClickedButtonIndex:0 animated:YES];
}
-(UIImage *)resizeImage:(UIImage *)image imageSize:(CGSize)size
{
UIGraphicsBeginImageContext(size);
[image drawInRect:CGRectMake(0,0,size.width,size.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
// here is the scaled image which has been changed to the size specified
UIGraphicsEndImageContext();
return newImage;
}
How can I add Scroll view so that I can scroll image or How can I change size of UIAlertView or is it is possible to change the size of UIAlertView.Thanks in Advance!
I give you what you ask.Your view name is customView and you set frame for that.Now the code looks like below.
UILongPressGestureRecognizer *longPressOnCustomView = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longPressOnCustomView:)];
longPressOnCustomView.delegate = self;
customView.userInteractionEnabled = YES;
[customView addGestureRecognizer:longPressOnCustomView];
#pragma mark Long Press Gesture Recognizer
- (void)longPressOnCustomView:(UILongPressGestureRecognizer*)gesture
{
if (gesture.state == UIGestureRecognizerStateEnded)
{
//Code for scrolling the image
}
}
Related
I created a method called ellipses in UIView and this method is creating an actionsheet.I tried to call this method in my UIViewController but it failed because of "[actionSheet showInView:self]".This actionsheet works fine in my view because of 'self' but I dont know how to fix the 'self' so that it can show both in my VC and my View.
here is part of my code in UIView
- (void)ellipses{
NSString *deleteButtonName = NSLocalizedString(#"Delete", #"");
NSString *copyButtonName = NSLocalizedString(#"Copy to clipboard", #"");
NSString *cancel = NSLocalizedString(#"Cancel", #"");
NSArray *buttons;
buttons = [NSArray arrayWithObjects:deleteButtonName, copyButtonName, nil];
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
for(NSString *title in buttons) {
[actionSheet addButtonWithTitle:title];
}
[actionSheet addButtonWithTitle:cancel];
[actionSheet setCancelButtonIndex:[buttons count]];
[actionSheet showInView:self]; // here is gonna be a problem later
}
here is part of code in my UIViewController
-(void)loadView{
newView *nView = [newView new];
[self setView:nView];
UIButton *testButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[testButton setTitle:#"Test" forState:UIControlStateNormal];
[testButton setFrame:CGRectMake(200, 230, 60, 28)];
[testButton setUserInteractionEnabled:YES];
[testButton addTarget:self action:#selector(actionSh) forControlEvents:UIControlEventTouchUpInside];
[nView addSubview:testButton];
}
-(void)actionSh{
newView *newview = [[newView alloc] init];
[newview ellipses];//CALL ellipses
[newview setNeedsDisplay];
}
The Answer for my question:
add this method in my VC.newView is my UIView.
- (newView *) newview {
return (newView *)[self view];
}
and call methods from my VC
-(void)actionSH {
[[self newview] ellipses];
}
I'm attempting to write an application that shows the camera feed, and you can take an image from it. I've successfully set up a UIButton and a linked action method (IBAction) but I'm having trouble making it trigger the UI to save the picture. I have it all set up so when everything goes correctly it should inform the user, but instead it crashes. Any insight would be greatly appreciated.
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(applicationDidBecomeActive)
name:UIApplicationDidBecomeActiveNotification
object:nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIApplicationDidBecomeActiveNotification
object:nil];
}
- (void)applicationDidBecomeActive
{
//Start the camera up
UIImagePickerController *imageViewPickerController = [[UIImagePickerController alloc] init];
//Hide default UI elements
imageViewPickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
imageViewPickerController.showsCameraControls = NO;
imageViewPickerController.navigationBarHidden = YES;
imageViewPickerController.toolbarHidden = YES;
//Start the button overlay
UIView *btnView = [[UIView alloc] initWithFrame:imageViewPickerController.view.bounds];
btnView.opaque = NO;
btnView.clipsToBounds = YES;
//Start up the button
UIButton *snapButton = [[UIButton alloc] initWithFrame:CGRectMake(-6, 504, 65, 65)];
snapButton.alpha = 0.5;
snapButton.transform = CGAffineTransformMakeRotation(DEGREES_RADIANS(90));
[snapButton addTarget:snapButton action:#selector(takePhoto:) forControlEvents:UIControlEventTouchUpInside];
//Set the button's picture
UIImage *snapButtonImage = [UIImage imageNamed:#"Button.png"];
[snapButton setImage:snapButtonImage forState:UIControlStateNormal];
//Add button to the overlay
[btnView addSubview:snapButton];
//Overlay button view
imageViewPickerController.cameraOverlayView = btnView;
//Fix for iPhone 5 and make fullscreen
CGAffineTransform translate = CGAffineTransformMakeTranslation(0.0, -55.0);
CGAffineTransform scale = CGAffineTransformMakeScale(1.333333, 1.333333);
CGAffineTransform rotate = CGAffineTransformMakeRotation(DEGREES_RADIANS(180));
CGAffineTransform transform = CGAffineTransformConcat(translate, scale);
transform = CGAffineTransformConcat(transform, rotate);
imageViewPickerController.cameraViewTransform = transform;
//Let's present it all
[self presentViewController:imageViewPickerController
animated:NO
completion:NULL];
}
- (IBAction)takePhoto:(id)sender {
[UIImagePickerController takePicture];
}
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
// Access the uncropped image from info dictionary
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
// Save image
UIImageWriteToSavedPhotosAlbum(image, self, #selector(image:didFinishSavingWithError:contextInfo:), nil);
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
UIAlertView *alert;
// Unable to save the image
if (error)
alert = [[UIAlertView alloc] initWithTitle:#"Error"
message:#"Unable to save image to Photo Album."
delegate:self cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
else // All is well
alert = [[UIAlertView alloc] initWithTitle:#"Success"
message:#"Image saved to Photo Album."
delegate:self cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alert show];
}
EDIT:
Revised code to resolve exception.
- (void)applicationDidBecomeActive
{
//Start the camera up
_imageViewPickerController = [[UIImagePickerController alloc] init];
//Hide default UI elements
_imageViewPickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
_imageViewPickerController.showsCameraControls = NO;
_imageViewPickerController.navigationBarHidden = YES;
_imageViewPickerController.toolbarHidden = YES;
//Start the button overlay
UIView *btnView = [[UIView alloc] initWithFrame:_imageViewPickerController.view.bounds];
btnView.opaque = NO;
btnView.clipsToBounds = YES;
//Start up the button
UIButton *snapButton = [[UIButton alloc] initWithFrame:CGRectMake(-6, 504, 65, 65)];
snapButton.alpha = 0.5;
snapButton.transform = CGAffineTransformMakeRotation(DEGREES_RADIANS(90));
[snapButton addTarget:snapButton action:#selector(snapThat) forControlEvents:UIControlEventTouchUpInside];
//Set the button's picture
UIImage *snapButtonImage = [UIImage imageNamed:#"Button.png"];
[snapButton setImage:snapButtonImage forState:UIControlStateNormal];
//Add button to the overlay
[btnView addSubview:snapButton];
//Overlay button view
_imageViewPickerController.cameraOverlayView = btnView;
//Fix for iPhone 5 and make fullscreen
CGAffineTransform translate = CGAffineTransformMakeTranslation(0.0, -55.0);
CGAffineTransform scale = CGAffineTransformMakeScale(1.333333, 1.333333);
CGAffineTransform rotate = CGAffineTransformMakeRotation(DEGREES_RADIANS(180));
CGAffineTransform transform = CGAffineTransformConcat(translate, scale);
transform = CGAffineTransformConcat(transform, rotate);
_imageViewPickerController.cameraViewTransform = transform;
//Let's present it all
[self presentViewController:_imageViewPickerController
animated:NO
completion:NULL];
}
- (void)snapThat {
[_imageViewPickerController takePicture];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
// Access the uncropped image from info dictionary
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
// Save image
UIImageWriteToSavedPhotosAlbum(image, self, #selector(image:didFinishSavingWithError:contextInfo:), nil);
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
UIAlertView *alert;
// Unable to save the image
if (error)
alert = [[UIAlertView alloc] initWithTitle:#"Error"
message:#"Unable to save image to Photo Album."
delegate:self cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
else // All is well
alert = [[UIAlertView alloc] initWithTitle:#"Success"
message:#"Image saved to Photo Album."
delegate:self cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alert show];
}
EDIT 2: Including headers
#interface TurnikitViewController : UIViewController <UINavigationControllerDelegate, UIImagePickerControllerDelegate>
#property (strong, nonatomic) IBOutlet UIView *imageView;
#property (strong, nonatomic) UIImagePickerController *imageViewPickerController;
#end
You should have this as property:
#property (strong, nonatomic) UIImagePickerController *imageViewPickerController;
then you init that as you are doing:
_imageViewPickerController = [[UIImagePickerController alloc] init];
and when you save:
[_imageViewPickerController takePicture];
Instead now, you are not calling the method on the instance, but on the class..that is an error:
[UIImagePickerController takePicture];
And also, you are adding as target of your button, itself..instead the target is self:
[snapButton addTarget:self action:#selector(snapThat) forControlEvents:UIControlEventTouchUpInside];
I am trying to move a UIImage, first button press creates the image and second press moves it.
The image only needs to exist upon pressing the button.
In the simulator it creates the button and places it, the second time it click just doesn't do anything.
This is my Code
- (IBAction) btn:(id)sender {
UIImageView *myImage = [[UIImageView alloc] init];
myImage.image = [UIImage imageNamed:#"keyframe"];
if (startUp == 1){
//Create Image and add to view
myImage.frame = CGRectMake(200, 300, 10, 10);
myImage.image = [UIImage imageNamed:#"keyframe"];
[self.view addSubview:myImage];
//Set startUp to 0 and output rect value
startUp = 0;
NSLog(#"currentFrame %#", NSStringFromCGRect(myImage.frame));
}else if (startUp == 0){
//Change position, size and log to debug
myImage.frame = CGRectMake(500,100 ,20, 20);
NSLog(#"newFrame %#", NSStringFromCGRect(myImage.frame));
}
}
How do you programmatically move a programmatically added UIimage?
I tried changing the center value but that doesn't work either.
Try something like this – tested and working sample:
#import "ViewController.h"
#interface ViewController () {
BOOL startUp;
UIImageView *myImage;
}
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
startUp = YES;
}
- (IBAction)doWork:(id)sender {
if (startUp) {
UIImage *img = [UIImage imageNamed: #"keyframe"];
myImage = [[UIImageView alloc] initWithImage: img];
[myImage sizeToFit];
[myImage setCenter: CGPointMake(200, 300)];
[self.view addSubview: myImage];
startUp = NO;
} else {
[myImage setCenter: CGPointMake(400, 500)];
}
}
#end
I am trying to find which image the user tapped using a gesture recognizer.
The scrollview as well as the image views are all created dynamically.
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 100)];
scroll.pagingEnabled = YES;
int iPolaroidDimen = 200;
NSInteger numberOfViews = 10;
for (int i = 0; i < numberOfViews; i++) {
CGFloat xOrigin = i * self.view.frame.size.width;
UIImageView *polaroid = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"iconPol.png"]];
polaroid.frame = CGRectMake(xOrigin+(self.view.center.x/2)-(iPolaroidDimen/10), (self.view.center.y/2), iPolaroidDimen, iPolaroidDimen);
[scroll setShowsHorizontalScrollIndicator:NO];
polaroid.userInteractionEnabled = TRUE;
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imageTapped:)];
singleTap.numberOfTapsRequired = 1;
singleTap.numberOfTouchesRequired = 1;
[polaroid addGestureRecognizer:singleTap];
[polaroid setUserInteractionEnabled:YES];
[polaroid setAccessibilityIdentifier:[NSString stringWithFormat:#"%#%#",#"ImageView",[NSString stringWithFormat:#"%d",i]]];
[scroll addSubview:polaroid];
The gesture recognizer
- (void)imageTapped:(UIGestureRecognizer *)gestureRecognizer{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle: #"Guess what?"
message: #"An image was tapped"
delegate: self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK",nil];
[alert show];
}
- (void)imageTapped:(UIGestureRecognizer *)gestureRecognizer {
UIView *tappedView = gestureRecognizer.view;
}
You can give a pointer to each of the 10 images so you can access them later. You only need to add tap gesture to the top view which has all the 10 images, and then add the following example code in your method (in which I give each image a different name such as cat, cow ,etc. :]):
- (void)imageTapped:(UIGestureRecognizer *)gestureRecognizer
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle: #"Guess what?"
message: #"An image was tapped"
delegate: self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK",nil];
[alert show];
CGPoint point = [gestureRecognizer locationInView:self.view];
CGRect catRect = CGRectMake(self.cat.frame.origin.x, self.cat.frame.origin.y, self.cat.frame.size.width, self.cat.frame.size.height);
CGRect cowRect = CGRectMake(self.cow.frame.origin.x, self.cow.frame.origin.y, self.cow.frame.size.width, self.cow.frame.size.height);
if (CGRectContainsPoint(catRect, point)) {
NSLog(#"I am a cat!");
} else if (CGRectContainsPoint(cowRect, point)) {
NSLog(#"I am a cow!");
}
}
Essentially I have a UIAlertView that pops up. When a user selects a button it calls itself, and based on the button index it brings up a UIImageView subview. My question is, because the UIImageView takes up a good portion of the screen it lays on top of the UIAlertView, so the cancel button is unseen. I was wondering if I could create actions on the UIImageView so that if it is clicked or touched up inside, the UIAlertView/UIImageView would resign and disappear?
Someone please help, I've been tooling around this for hours.
Here is the code from the button being clicked, and the button index.
- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
// the user clicked one of the OK/Cancel buttons
if (buttonIndex == 0)
{
// exit(0);
}
else
{
UIAlertView *successAlert = [[UIAlertView alloc] initWithTitle:#"molecule" message:molURL delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 280, 260)];
NSString *MyURL = molURL;
NSString *apURL = [NSString stringWithFormat:#"%#",MyURL];
NSURL * imageURL = [NSURL URLWithString:apURL];
NSData * imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage * image1 = [UIImage imageWithData:imageData];
//UIImage *bkgImg = [[UIImage alloc] initWithContentsOfFile:path];
[imageView setImage:image1];
[successAlert addSubview:imageView];
[successAlert show];
}
}
MyImageView.h
#import <UIKit/UIKit.h>
#interface MyImageView : UIImageView {
}
#end
MyImageView.m
#import "MyImageView.h"
#implementation MyImageView
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.userInteractionEnabled=YES;
}
return self;
}
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[UIView animateWithDuration:2.0f animations:^(void) {
self.alpha=0.0f;
super.hidden =YES;
UIAlertView *alert=(UIAlertView*)self.superview;
alert.alpha=0.0f;
[alert dismissWithClickedButtonIndex:0 animated:YES];
//or
//[alert removeFromSuperview];
}];
}
- (void)dealloc {
[super dealloc];
}
#end
MyViewController.h
#class MyImageView;
MyViewController.m
#import "MyImageView.h"
then while create imageView
MyImageView *specialImageView =[[MyImageView alloc]initWithFrame:CGRectMake(0, 0, 280, 260)];