The older 3GS, or iPod 4 are able to show the final tempimage, but on the iPhone 4, I don't see the tempview showing up on screen.
I stepped through it, getting imageData, tempimage allocated, tempview allocated, but the final tempview is not showing up.
-(void) display:(NSData*) imageData
{
tempimage= [[[UIImage alloc] initWithData:imageData] autorelease];
tempview=[[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)] autorelease];
if (tempimage) {
[tempview setImage:tempimage];
[self.view addSubview:tempview];
}
try like this..
-(void) display:(NSData*) imageData
{
if(imageData != nil)
{
tempimage= [[UIImage alloc] initWithData:imageData];
}
else
{
tempimage= [UIImage imageNamed:#"something.png"];
}
tempview=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
if (tempimage) {
[tempview setImage:tempimage];
[self.view addSubview:tempview];
}
if(imageData != nil)
{
[tempimage release];
}
[tempview release];
}
Related
I am adding UIImageView on UIScrollView but my app getting crash when array count is high.
Here is my code
scr=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 60, 320, 320)];
scr.tag=1000;
[scr setDelegate:self];
[self.view addSubview:scr];
dispatch_queue_t queeee =dispatch_queue_create("raj", 0);
int countt=200;
for(int i=0;i<countt;i++)
{
CGRect rectt=CGRectMake((i%3*90)+30,(i/3*90)+40,80,70);
UIImageView *asyImagee = [[UIImageView alloc]initWithFrame:rectt];
dispatch_async(queeee, ^{
NSData *data=[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"Default" ofType:#"png"]];
__block UIImage *imgg=[UIImage imageWithData:data];
dispatch_async(dispatch_get_main_queue(), ^
{
asyImagee.image=imgg;
imgg=nil;
});
});
asyImagee.tag=i;
[scr addSubview:asyImagee];
asyImagee=nil;
}
[scr setContentSize:CGSizeMake(0, countt/3*110)];
Working fine with int countt=100;
but crash with 200;
How i can save images from this scrollview in photo library when user longpress on imageview.
Getting yellow warning for statement
UIImageWriteToSavedPhotosAlbum(_imageScrollView, self, nil, nil);
How can i resolve this warning to save image on photo library
- (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)];
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);
UILongPressGestureRecognizer *gestureRecognizer = [[UILongPressGestureRecognizer alloc]
initWithTarget:self
action:#selector(handleLongPress:)];
gestureRecognizer.minimumPressDuration = 2.0;
imageScrollView.userInteractionEnabled = YES;
[imageScrollView addGestureRecognizer:gestureRecognizer];
gestureRecognizer.delegate = self;
[gestureRecognizer release];
[imageScrollView addSubview:imageView];
[imageScrollView addSubview:myButton];
}
imageScrollView.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
[self.view addSubview:imageScrollView];
[imageScrollView release];
}
- (void)handleLongPress:(UILongPressGestureRecognizer*)gestureRecognizer{
if (gestureRecognizer.state == UIGestureRecognizerStateEnded){
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Save Photo", nil];
actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
[actionSheet showInView:self.view];
[actionSheet release];
}
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
switch (buttonIndex) {
case 0:
UIImageWriteToSavedPhotosAlbum(_imageScrollView, self, nil, nil);
break;
default:
break;
}
}
Getting warning in yellow on this statement
UIImageWriteToSavedPhotosAlbum(_imageScrollView, self, nil, nil);
that incompatible pointer types passing UIScrollView to parameter of type UIImage
Thanks
see
this : saving image
and
this :download and saving image
but this one have great rating prev. answer
In below code there is about 3MB of leak.If I remove [self.view addSubview:progressDialog];
then their is no leak.
-(void)showProgressDialog:(NSString*)title setTimer:(BOOL)isTimerSet
{
progressDialog = [[MBProgressHUD alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
[progressDialog setLabelText:title];
progressDialog.dimBackground=YES;
[self.view addSubview:progressDialog];//Leak is here
[progressDialog show:YES];
}
-(void)hideProgressDialog
{
if(progressDialog !=nil)
{
[progressDialog hide:YES];
[progressDialog removeFromSuperview];
[progressDialog release];
progressDialog = nil;
}
}
Please help.
you are not releasing it progressDialog or if it is ivar, then use property instead of ivar, and synthesize it, then follow this approch
MBProgressHUD *obj = [[MBProgressHUD alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
self.progressDialog=obj;
[obj release];
like this
MBProgressHUD *obj = [[MBProgressHUD alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
self.progressDialog=obj;
[obj release];
[self.progressDialog setLabelText:title];
self.progressDialog.dimBackground=YES;
[self.view addSubview:self.progressDialog];//Leak is here
[self.progressDialog show:YES];
working on an app and using the following code:
- (void)viewDidLoad {
[super viewDidLoad];
dataSource = [[NSArray alloc] initWithContentsOfFile:[[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:#"stories.plist"]];
contentsHeaderImageView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"ContentsHeader.png"]] autorelease];
[contentsHeaderImageView setFrame:CGRectMake(0, 0, 320, 131)];
[contentsHeaderImageView setUserInteractionEnabled:YES];
[self.view addSubview:contentsHeaderImageView];
UITableView *tableView = [[[UITableView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(contentsHeaderImageView.frame), self.view.frame.size.width, self.view.frame.size.height - CGRectGetMaxY(contentsHeaderImageView.frame))] autorelease];
[tableView setDelegate:self];
[tableView setDataSource:self];
[tableView setAutoresizingMask:UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth];
[tableView setContentInset:UIEdgeInsetsMake(1, 0, 1, 0)];
[tableView setSeparatorColor:[UIColor whiteColor]];
[self.view addSubview:tableView];
[self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"background.png"]]];
[tableView setBackgroundColor:[UIColor clearColor]];
UIImageView *newView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"fade.png"]] autorelease];
[newView setFrame:CGRectMake(0, -20, 320, 69)];
[self.view addSubview:newView];
splashImage = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Default.png"]] autorelease];
[splashImage setFrame:CGRectMake(0, -20, 320, 480)];
[self.view addSubview:splashImage];
[self performSelector:#selector(animateOutSplashImage:) withObject:splashImage afterDelay:3];
}
-(void)drawBookmarkButton
{
if (self.bookmarkButton) {
[self.bookmarkButton removeFromSuperview];
}
if ([[NSUserDefaults standardUserDefaults] objectForKey:#"bookmark"]) {
self.bookmarkButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.bookmarkButton setImage:[UIImage imageNamed:#"bookmark 1.png"] forState:UIControlStateNormal];
[contentsHeaderImageView addSubview:self.bookmarkButton];
[self.bookmarkButton addTarget:self action:#selector(bookmarkButtonTapped) forControlEvents:UIControlEventTouchUpInside];
[self.bookmarkButton setFrame:CGRectMake(260, 0, 50, 35)];
//UITapGestureRecognizer *gr = [[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(bookmarkButtonTapped)] autorelease];
//[self.bookmarkButton addGestureRecognizer:gr];
}
}
-(void)animateOutSplashImage:(UIImageView *)splashImg
{
[UIView animateWithDuration:1 animations:^{
[splashImage setAlpha:0];
}completion:^(BOOL finished){
[splashImage removeFromSuperview];
splashImage = nil;
//[self drawBookmarkButton];
}];
}
-(void)viewWillAppear:(BOOL)animated
{
[self drawBookmarkButton];
}
For some reason the first time the app is run the bookmark button won't respond to taps, but if I push a view controller then go back it's working fine. Spent a while trying to figure this out and nothing is working.
Any ideas???
Thanks!
did you try
[contentsHeaderImageView bringSubViewToFront:self.bookmarkButton];
also create a UIView instance for headerView and add imageView and button into the view.
Apologies, it was (as usual with me!) a stupid mistake!
In the delegate I set:
self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
self.navigationController.navigationBar.translucent = YES;
and must have forgot about it. Setting the nav bar to hidden instead meant that the nav bar wasn't blocking the button.
Thanks for your help!
I'm having trouble deallocating a UISCrollView that contains images.
There's something wrong with my code, but I cannot understand where.
this is a snippet of my code. It basically create a loops adding a uiscrollview with an image and removing.
If you run the code with instruments, no memory will be freed.
I also add some check for the retainCount, with no luck at all..
here's the code
- (void)loadView {
[super loadView];
CGRect theRect = CGRectMake(0, 0, 320, 480);
UIView *view = [[UIView alloc] initWithFrame:theRect];
[view setBackgroundColor:[UIColor purpleColor] ];
self.view = view;
[view release];
UIView *pippo = [[UIView alloc] initWithFrame:theRect];
[pippo setBackgroundColor:[UIColor redColor]];
[self.view addSubview:pippo];
[pippo release];
[self performSelector:#selector(scrollAdd:) withObject:nil afterDelay:2.0f];
}
-(void)scrollAdd:(id)o {
CGRect theRect = CGRectMake(0, 0, 320, 480);
int numero = 1;
scroll = [[UIScrollView alloc] initWithFrame:theRect];
[scroll setContentSize:CGSizeMake( 320*numero,1)];
[scroll setScrollEnabled:YES];
UIImage *img = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:#"koala1b" ofType:#"jpg"]];
int dd = [img retainCount];
UIImageView *v2 = [[UIImageView alloc] initWithFrame:theRect];
[v2 setImage:img];
[scroll addSubview:v2];
dd = [v2 retainCount];
[v2 release];
dd = [v2 retainCount];
dd = [img retainCount];
[self.view addSubview:scroll];
[img release];
dd = [img retainCount];
[self performSelector:#selector(scrollRemove:) withObject:nil afterDelay:2.0f];
}
-(void)scrollRemove:(id)o {
int dd = [scroll retainCount];
UIImageView *theV = [[scroll subviews] objectAtIndex:0];
dd = [theV retainCount];
[scroll removeFromSuperview];
dd = [theV retainCount];
[theV release];
dd = [theV retainCount];
dd = [scroll retainCount];
scroll = nil;
[self performSelector:#selector(scrollAdd:) withObject:nil afterDelay:2.0f];
}
The issue is that you are releasing img when you shouldn't (which is probably being masked by the view hierarchy never being released), and you aren't releasing scroll when you should.
-(void)scrollAdd:(id)o {
CGRect theRect = CGRectMake(0, 0, 320, 480);
int numero = 1;
scroll = [[UIScrollView alloc] initWithFrame:theRect];
[scroll setContentSize:CGSizeMake( 320*numero,1)];
[scroll setScrollEnabled:YES];
UIImage *img = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:#"koala1b" ofType:#"jpg"]];
int dd = [img retainCount];
UIImageView *v2 = [[UIImageView alloc] initWithFrame:theRect];
[v2 setImage:img];
[scroll addSubview:v2];
[v2 release];
[self.view addSubview:scroll];
[img release];
dd = [img retainCount];
[self performSelector:#selector(scrollRemove:) withObject:nil afterDelay:2.0f];
}
This should be:
-(void)scrollAdd:(id)o {
CGRect theRect = CGRectMake(0, 0, 320, 480);
int numero = 1;
scroll = [[UIScrollView alloc] initWithFrame:theRect];
[scroll setContentSize:CGSizeMake( 320*numero,1)];
[scroll setScrollEnabled:YES];
UIImage *img = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:#"koala1b" ofType:#"jpg"]];
UIImageView *v2 = [[UIImageView alloc] initWithFrame:theRect];
[v2 setImage:img];
[scroll addSubview:v2];
[v2 release];
[self.view addSubview:scroll];
[self performSelector:#selector(scrollRemove:) withObject:nil afterDelay:2.0f];
}
Of course if you do this you need to also make a slight change in your view removal path:
-(void)scrollRemove:(id)o {
[scroll removeFromSuperview];
[scroll release];
scroll = nil;
[self performSelector:#selector(scrollAdd:) withObject:nil afterDelay:2.0f];
}
I've basically reached the same conclusions as #Louis, but also made some comments within the code as to what I removed and why.
- (void)loadView {
[super loadView];
CGRect theRect = CGRectMake(0, 0, 320, 480);
UIView *view = [[UIView alloc] initWithFrame:theRect];
[view setBackgroundColor:[UIColor purpleColor] ];
self.view = view;
[view release];
UIView *pippo = [[UIView alloc] initWithFrame:theRect];
[pippo setBackgroundColor:[UIColor redColor]];
[self.view addSubview:pippo];
[pippo release];
[self performSelector:#selector(scrollAdd:) withObject:nil afterDelay:2.0f];
}
-(void)scrollAdd:(id)o {
CGRect theRect = CGRectMake(0, 0, 320, 480);
int numero = 1;
scroll = [[UIScrollView alloc] initWithFrame:theRect];
[scroll setContentSize:CGSizeMake( 320*numero,1)];
[scroll setScrollEnabled:YES];
// img is already autoreleased, you're releasing again down below
// --- UIImage *img = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:#"koala1b" ofType:#"jpg"]];
UIImageView *v2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"koala1b.jpg"]];
v2.frame = theRect;
//--- [v2 setImage:img];
[scroll addSubview:v2];
[v2 release];
[self.view addSubview:scroll];
// NO !; img is autoreleased from imageWithContentsOfFile
//--- [img release];
[self performSelector:#selector(scrollRemove:) withObject:nil afterDelay:2.0f];
}
-(void)scrollRemove:(id)o {
//--- UIImageView *theV = [[scroll subviews] objectAtIndex:0];
[scroll removeFromSuperview];
// you don't own theV because you didn't create it here, or send it a retain. So NO release
// it also appears that you think you're responsible for releasing or cleaning up
// a view's subviews. NO. Just call [scroll removeFromSuperview] and let scroll view
// clean it's own resources.
//--- [theV release];
[scroll release];
scroll = nil;
[self performSelector:#selector(scrollAdd:) withObject:nil afterDelay:2.0f];
}