I have a blank screen on which i click camera button and take photo and than that photo appears on my blank screen as a UIVIew and i'm adding multiple images from Camera to blank screen. The problem is sometimes i can add multiple images on blank screen but sometimes when i capture image and than all images on blank screen disappear and just shows the current image.
- (IBAction)openCameraOnAddButton:(id)sender {
//NSLog(#"openCameraOnAddButton");
[AddImagesToCanvasView setHidden:YES];
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:picker animated:YES];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissModalViewControllerAnimated:YES];
UIImage *image = [[info objectForKey:#"UIImagePickerControllerOriginalImage"] retain];
UIView *holderView;
if(image.size.width > image.size.height || image.size.width == image.size.height)
{
holderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 180, 160)];
}
else{
holderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 160, 240)];
}
UIImageView *imageview = [[UIImageView alloc] initWithFrame:[holderView frame]];
[imageview setImage:image];
NSLog(#"Tag By Default %d",(arc4random()%100)+10);
[holderView setTag:(int)objectDelegate.tagForHolderView];
[imageview setTag:((int)objectDelegate.tagForHolderView)+1];
[holderView addSubview:imageview];
UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(scale:)];
[pinchRecognizer setDelegate:self];
[holderView addGestureRecognizer:pinchRecognizer];
UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:#selector(rotate:)];
[rotationRecognizer setDelegate:self];
[holderView addGestureRecognizer:rotationRecognizer];
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[holderView addGestureRecognizer:panRecognizer];
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapped:)];
[tapRecognizer setNumberOfTapsRequired:1];
[tapRecognizer setDelegate:self];
[holderView addGestureRecognizer:tapRecognizer];
UILongPressGestureRecognizer *gestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(gestureHandler:)];
[holderView addGestureRecognizer:gestureRecognizer];
[holderView.layer setBorderColor: [[UIColor whiteColor] CGColor]];
[holderView.layer setBorderWidth: 3.0];
[self.view addSubview:holderView];
objectDelegate.tagForHolderView+=2;
}
Not sure but as you are doing all the operation UIImagePickerCotroller's delegate methods sometimes it may not work.
I faced same problem while storing the image in document directory & to resolve this i had created new Thread in didFinishPickingMediaWithInfo method & moved entire code in that method. Then after it works fine for me. Try to do that.
Related
bigLabel = [[UILabel alloc] init];
UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapAction)];
[tap setNumberOfTapsRequired:1];
[bigLabel addGestureRecognizer:tap];
bigLabel.backgroundColor=[UIColor clearColor];
bigLabel.text = _referenceObject.textForCell;
//bigLabel.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:#"Header1.png"]];
bigLabel.font = [UIFont fontWithName:#"HelveticaNeueLTStd-Bd" size: 21.0];
bigLabel.font =[UIFont boldSystemFontOfSize:21.0f];
bigLabel.textColor = [UIColor whiteColor];
[bigLabel sizeToFit];
[self.navigationItem setTitleView:bigLabel];
Use the following code :
UITapGestureRecognizer* tapRecon = [[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(navigationBarTap:)];
tapRecon.numberOfTapsRequired = 1;
[navController.navigationBar addGestureRecognizer:tapRecon];
you need to set
bigLabel.userInteractionEnabled = YES;
because by default UILabel instances userInteractionEnabled is NO
Try setting a frame to it like so:
UIView *iv = [[UIView alloc] initWithFrame:CGRectMake(0,0,32,32)];
[iv setBackgroundColor:[UIColor whiteColor]];
self.navigationItem.titleView = iv;
UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(hideKeyBoard)];
tapGesture.cancelsTouchesInView = NO;
[self.navigationController.view addGestureRecognizer:tapGesture];
-(void)hideKeyBoard
{
[self.view endEditing:YES];
}
I have a blank screen on which i click camera button and take photo and than that photo appears on my blank screen as a UIVIew and i'm adding multiple images from Camera to blank screen. The problem is sometimes i can add multiple images on blank screen but sometimes when i capture image and than all images on blank screen disappear and just shows the current image.
And in LOG. it show me Received memory warning.
this is my code. i think i m now releasing my objects properly. but when i am adding [object release] in the end my app crashes.
-(void)AddImagesToCanvasWithGesture{
if(imageFromPicker.size.width > imageFromPicker.size.height || imageFromPicker.size.width == imageFromPicker.size.height)
{
holderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 180, 160)];
}
else{
holderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 160, 240)];
}
UIImageView *imageview = [[[UIImageView alloc] initWithFrame:[holderView frame]];
[imageview setImage:imageFromPicker];
//[imageview setTag:101];
//[holderView setTag:102];
//NSLog(#"Tag By Default %d",(arc4random()%100)+10);
[holderView setTag:(int)objectDelegate.tagForHolderView];
[imageview setTag:((int)objectDelegate.tagForHolderView)+1];
[holderView addSubview:imageview];
//[imageview canBecomeFirstResponder];
UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(scale:)];
[pinchRecognizer setDelegate:self];
[holderView addGestureRecognizer:pinchRecognizer];
UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:#selector(rotate:)];
[rotationRecognizer setDelegate:self];
[holderView addGestureRecognizer:rotationRecognizer];
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[holderView addGestureRecognizer:panRecognizer];
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapped:)];
[tapRecognizer setNumberOfTapsRequired:1];
[tapRecognizer setDelegate:self];
[holderView addGestureRecognizer:tapRecognizer];
UILongPressGestureRecognizer *gestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(gestureHandler:)];
[holderView addGestureRecognizer:gestureRecognizer];
[holderView.layer setBorderColor: [[UIColor whiteColor] CGColor]];
[holderView.layer setBorderWidth: 3.0];
[self.view addSubview:holderView];
objectDelegate.tagForHolderView+=2;
for(UIButton *b in self.view.subviews) {
if([b isKindOfClass:[UIButton class]]) {
[self.view bringSubviewToFront:b];
}
[self.view sendSubviewToBack:imageViewForBackground];
//[holderView setHidden:YES];
}
}
how can i release them and where should i call the release method.
From the code snippet you have added above, it's obvious the you are not releasing objects properly. You own every object that you init or retain or copy and you are responsible to release them unless you are using ARC. (The code above is fine for ARC).
You have ownership of all the gesture recognizers and image view and you should release them as soon as you no longer need them. For instance... imageView object must be released after
[holderView addSubview:imageview];
and
pinchRecognizer should be released right after
[holderView addGestureRecognizer:pinchRecognizer];
Same goes for other gesture recognizers.
I guess you have similar kind of problems in other parts of your code too and eventually the app receives memory warning.
When you have a memory problem, if you have a method named
- (void)didReceiveMemoryWarning
it will get called and allow you to free up space. If you are using ARC, just set your UIImageView to nil, if not, call:
[imageview release];
I am using Apple code for just zoom the UIImageView on taps and gestures. But its not work ?
Please see this Apple's link apple code for image zooming by taps and gestures
-(void)veiwDidLoad
{
[super viewDidLoad];// Edited my self
imageView.userInteractionEnabled = YES; // Edited my self
//All code below same.... like Apple's code
// set the tag for the image view
[imageView setTag:ZOOM_VIEW_TAG];
// add gesture recognizers to the image view
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleDoubleTap:)];
UITapGestureRecognizer *twoFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTwoFingerTap:)];
[doubleTap setNumberOfTapsRequired:2];
[twoFingerTap setNumberOfTouchesRequired:2];
[imageView addGestureRecognizer:singleTap];
[imageView addGestureRecognizer:doubleTap];
[imageView addGestureRecognizer:twoFingerTap];
[singleTap release];
[doubleTap release];
[twoFingerTap release];
// calculate minimum scale to perfectly fit image width, and begin at that scale
float minimumScale = [imageScrollView frame].size.width / [imageView frame].size.width;
[imageScrollView setMinimumZoomScale:minimumScale];
[imageScrollView setZoomScale:minimumScale];
}
Make sure that you set your imageView to be userInteractionEnabled = YES; .. its assign to NO by default.
change the function name from
-(void) veiwDidLoad
to
-(void) viewDidLoad
I has a problem like that as well, does that fix it?
I encountered a problem with my UILongPressGestureRecognizer and UIPanGestureRecognizer.
i wish to long press in my self.view to add a view with UIPanGestureRecognizer in my self.view.
However the UIPanGestureRecognizer is not recognizing.
did i missed anything ?
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//[self initImagesAndGesture];
UILongPressGestureRecognizer *tapRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(addImgView:)];
tapRecognizer.numberOfTouchesRequired = 1;
tapRecognizer.minimumPressDuration = 0.7;
[tapRecognizer setDelegate:self];
self.view.userInteractionEnabled = YES;
[self.view addGestureRecognizer:tapRecognizer];
}
-(void)addImgView:(UILongPressGestureRecognizer *)recognizer
{
NSLog(#"tappppp");
if(UIGestureRecognizerStateBegan == recognizer.state) {
// Called on start of gesture, do work here
NSLog(#"UIGestureRecognizerStateBegan");
UIImage *img = [UIImage imageNamed:#"beer.png"];
UIImageView *imgView = [[UIImageView alloc]initWithImage:img];
UILabel *timeStamp = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
timeStamp.text = [NSString stringWithFormat:#"%f",[NSDate date]];
UIView *drinkView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
[drinkView setUserInteractionEnabled:YES];
CGPoint tapLocation = [recognizer locationInView:recognizer.view];
[timeStamp setCenter:CGPointMake(tapLocation.x, tapLocation.y)];
[imgView setCenter:CGPointMake(tapLocation.x,tapLocation.y)];
[drinkView addSubview:imgView];
[drinkView addSubview:timeStamp];
[drinkView setUserInteractionEnabled:YES];
[imgView setUserInteractionEnabled:YES];
[timeStamp setUserInteractionEnabled:YES];
UIPanGestureRecognizer *recognizer1 = [[UIPanGestureRecognizer alloc]initWithTarget:self action:#selector(handlePan1:)];
[recognizer1 setDelegate:self];
[drinkView addGestureRecognizer:recognizer1];
[self.view addSubview:drinkView];
}
}
-(void)addImgView:(UILongPressGestureRecognizer *)recognizer
{
NSLog(#"tappppp");
if(UIGestureRecognizerStateBegan == recognizer.state) {
// Called on start of gesture, do work here
NSLog(#"UIGestureRecognizerStateBegan");
UIImage *img = [UIImage imageNamed:#"beer.png"];
UIImageView *imgView = [[UIImageView alloc]initWithImage:img];
UILabel *timeStamp = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 150, 30)];
timeStamp.text = [NSString stringWithFormat:#"%f",[NSDate date]];
UIView *drinkView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 75,115)];
drinkView.backgroundColor=[UIColor redColor];
[drinkView setUserInteractionEnabled:YES];
CGPoint tapLocation = [recognizer locationInView:recognizer.view];
[drinkView setCenter:CGPointMake(tapLocation.x,tapLocation.y)];
[drinkView setUserInteractionEnabled:YES];
[imgView setUserInteractionEnabled:YES];
[timeStamp setUserInteractionEnabled:YES];
[drinkView addSubview:imgView];
[drinkView addSubview:timeStamp];
UIPanGestureRecognizer *recognizer1 = [[UIPanGestureRecognizer alloc]initWithTarget:self action:#selector(handlePan1:)];
[recognizer1 setDelegate:self];
[drinkView addGestureRecognizer:recognizer1];
[self.view addSubview:drinkView];
}
if(UIGestureRecognizerStateChanged == recognizer.state) {
// Do repeated work here (repeats continuously) while finger is down
NSLog(#"UIGestureRecognizerStateChanged");
}
if(UIGestureRecognizerStateEnded == recognizer.state) {
// Do end work here when finger is lifted
NSLog(#"UIGestureRecognizerStateEnded");
}
}
While I know how to use gesture recognizer in a view-based application,but when I apply the same ideas in a OpenGLSE-based application:
for example,
I add a TapGestureRecognizer,and when I tap on the EAGLView,it crashes.
So can anyone show me a standard usage of UITapGestureRecognizer in an OpenGLES-based application?
best wishes.
Here some sample code from one of my opengles games with gesture support. (Doesn't crash and hope it helps)
- (void)viewDidLoad {
[super viewDidLoad];
CGRect rect = [[UIScreen mainScreen] bounds];
rect.size.height = 320;
rect.size.width = 480;
rect.origin.x = 0;
rect.origin.y = 0;
glView = [[EAGLView alloc] initWithFrame:rect pixelFormat:GL_RGB565_OES depthFormat:GL_DEPTH_COMPONENT16_OES preserveBackbuffer:NO];
[self.view addSubview: glView];
[glView addSubview: minimapView];
if(!shell->InitApplication())
printf("InitApplication error\n");
[NSTimer scheduledTimerWithTimeInterval:(1.0 / kFPS) target:self selector:#selector(update) userInfo:nil repeats:YES];
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(Panned:)];
[glView addGestureRecognizer:[pan autorelease]];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(Tapped:)];
[glView addGestureRecognizer:[tap autorelease]];
UITapGestureRecognizer *dbltap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(DoubleTapped:)];
[dbltap setNumberOfTapsRequired:2];
[glView addGestureRecognizer:[dbltap autorelease]];
UILongPressGestureRecognizer *longpress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(LongPressed:)];
[glView addGestureRecognizer:[longpress autorelease]];
}
And the selector function
- (void) LongPressed:(UILongPressGestureRecognizer*)sender{
NSLog(#"Long Pressed");
}