iOS: cant view downloaded images - ios

I'm a novice in this, so i would like to know what is missing in my code to download/show these images:
self.carView.contentSize = CGSizeMake(280*self.carImages.count-280, 200);
self.carView.pagingEnabled = YES;
CGRect imageRect = CGRectMake(0.0, 0.0,280*self.carImages.count-280, 200);
UIGraphicsBeginImageContext(imageRect.size);
UIImageView *carImg = [[UIImageView alloc] init];
for (int i = 1; i < self.carImages.count; i++) {
NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[self.carImages objectAtIndex:i]]];
UIImage *carImage = [[UIImage alloc] initWithData:imageData];
[carImage drawAtPoint:CGPointMake(280*(i-1), 0)];
[carImage release];
[imageData release];
NSLog(#"%d",i);
}
carImg.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[self.carView addSubview:carImg];
[carImg release];
carView is my ScrollView.
carImages is a MutableArray with the "URL" of the images.
the pagingEnabled and contentSize are being set, but no images.
I bet i'm missing something very stupid, as i allways do.

Related

How to zoom and swipe multiple images coming from webservise in objective c

1) In my application, I want to zoom and pinch an image. In xib file, there is a imageView inside a scrollview.Page control is used for swiping images. For a single image it gets zoomed, but for multiple images(coming from web service) it is not getting swipe and zoom.
In setMultipleImages method, images are not getting zoomed and swiped, while if image is single it gets zoom properly.
2) In UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageURL]]]; line code, want to replace uiimage with uiimageview. How can I do it?
My code is:
-(void)setMultipleImages{
for (int i = 0; i < [imageMArray count]; i++){
NSString *logoName;
NSArray *imageNameArray = [[imageMArray objectAtIndex:i] componentsSeparatedByString:#"/"];
logoName = [[NSString alloc]initWithFormat:#"%#",[imageNameArray lastObject]];
if (![[AppDelegateHelper getInstance] fileExist:logoName])
{
NSString *imageURL = [[NSString alloc]initWithFormat:#"%#%#",[UrlBuilder getWebsiteUrl],[imageMArray objectAtIndex:i]];
[self startProcessing];
UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageURL]]];
[activityIndicator stopAnimating];
DebugLog(#"%f,%f",image.size.width,image.size.height);
NSArray *docDir = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cacheDirectory = [docDir objectAtIndex:0];
DebugLog(#"saving png");
NSString *pngFilePath = [NSString stringWithFormat:#"%#/%#",cacheDirectory,[imageNameArray lastObject]];
NSData *data1 = [NSData dataWithData:UIImageJPEGRepresentation(image, 1.0f)];
[data1 writeToFile:pngFilePath atomically:YES];
}
}
[self loadImageData];
}
-(void)loadImageData
{
int X =0;
for (int s=0; s<imageMArray.count; s++) {
NSString *logoName;
NSArray *imageNameArray = [[imageMArray objectAtIndex:s] componentsSeparatedByString:#"/"];
logoName = [[NSString alloc]initWithFormat:#"%#",[imageNameArray lastObject]];
if([[AppDelegateHelper getInstance] loadImage:logoName] != nil)
{
// UIImageView *imageView = [[UIImageView alloc] initWithFrame: CGRectMake(X, 0, scrollView.frame.size.width, 300)] ;
[imageView setImage:[[AppDelegateHelper getInstance] loadImage:logoName]];
imageView.contentMode = UIViewContentModeScaleAspectFit;
[scrollView addSubview: imageView];
X = X + imageView.frame.size.width;
}
else
{
//UIImageView *imageView = [[UIImageView alloc] initWithFrame: CGRectMake(X, 0, scrollView.frame.size.width, 300)] ;
[imageView setImage:[UIImage imageNamed:#"NoImage.png"]];
imageView.contentMode = UIViewContentModeScaleAspectFit;
[scrollView addSubview: imageView];
X = X + imageView.frame.size.width;
}
}
self.pageControl.numberOfPages = [imageMArray count];
scrollView.contentSize = CGSizeMake(X,scrollView.frame.size.height);
lblCount.text = [[NSString alloc]initWithFormat:#"1 sur %lu",(unsigned long)imageMArray.count];
}
Add all your images to imageview. Then apply pinchguesture to images view for zoom effect.
Pinch Guesture:
UIPinchGestureRecognizer *imageviewguesture=[[UIPinchGestureRecognizer alloc]initWithTarget:self action:#selector(ImageViewPinchGuesture:)];
add pinch guesture to your image view:
[YourImageView addGestureRecognizer:imageviewguesture];
Make your selector method:
-(void)ImageViewPinchGuesture:(UIPinchGestureRecognizer *)pinGuestureregonizer{
pinGuestureregonizer.view.transform = CGAffineTransformScale(pinGuestureregonizer.view.transform, pinGuestureregonizer.scale, pinGuestureregonizer.scale);
pinGuestureregonizer.scale = 1;
}
In your .h file add this line
-(void)ImageViewPinchGuesture:(UIPinchGestureRecognizer *)pinGuestureregonizer;
To check effect in simulator Press Cmd+alt then press left mouse pad button and drag.
Hope this will Help. Don't forget to accept the answer if it helps you.

Image Iteration in iOS

Is there a better way to iterate the following images?
NSArray *imageNames = #[#"MyImage-0.png", #"MyImage-1.png", #"MyImage-2.png",#"MyImage-3.png",#"MyImage-4.png", #"MyImage-5.png", #"MyImage-6.png", #"MyImage-7.png"];
NSMutableArray *images = [[NSMutableArray alloc] init];
for (int i = 0; i < imageNames.count; i++) {
[images addObject:[UIImage imageNamed:[imageNames objectAtIndex:i]]];
}
mImageView.animationImages = images;
mImageView.animationDuration = 1.5;
[self.view mImageView];
[mImageView startAnimating];
NSInteger imagesCount = 7;
NSMutableArray *images = [[NSMutableArray alloc] init];
for (int i = 0; i <= imagesCount; i++) {
[images addObject:[UIImage imageNamed:[NSString stringWithFormat:#"MyImage-%d", i]]];
}
mImageView.animationImages = images;
mImageView.animationDuration = 1.5;
[self.view mImageView];
[mImageView startAnimating];
You can use enumeration
for(NSString *imageStr in imageNames)
{
[images addObject:[UIImage imageNamed:imageStr]];
}
This takes less time to iterate.
Hope it helps you...!
The better way will be not using for loop. Try this code
+(NSArray*)getSortedArray:(NSMutableArray*)arrayToSort
{
return [arrayToSort sortedArrayUsingSelector:#selector(compare:)];
}
Hope this class method will help you.
You might want to use some handy library, like this one. Look, it even supports GIF!
NSString *urlString = #"http://raphaelschaad.com/static/nyan.gif";
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]];
FLAnimatedImage *image = [FLAnimatedImage animatedImageWithGIFData:data];
FLAnimatedImageView *imageView = [[FLAnimatedImageView alloc] init];
imageView.animatedImage = image;
imageView.frame = CGRectMake(0.0, 0.0, 100.0, 100.0);
[self.view addSubview:imageView];

Loading images on scrollview using lazy loading

I am working on an app in which I need to load nearly 211 images in a scrollview.
What I was doing was I was converting the images in binary format (NSData) and saving them in core data. Then retrieving them and populating them on the scrollview.
it does work, but sometimes it does throw "Memory warning".
I learned that lazy loading is a way to achieve this. But, I am not totally aware of how it is done.
Like, loading 3 or 5 images ahead/back of the image visible currently on the scrollview.
Can any bode help me with a simple example, which would help me understand from scratch ? and would point me in the right direction.
Thank you for your time.
Well, it is not needed to store images into core data. Just take all image URLs into NSMutableArray and try to use following code as per your requirement.Hope this helps you.
Try this code -
friendsScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 470, 320, 100)];
friendsScrollView.contentSize = CGSizeMake([meetUPFrndNameArray count] * 95,50);
friendsScrollView.backgroundColor = [UIColor clearColor];
friendsScrollView.delegate = self;
[self.view addSubview:friendsScrollView];
int imageX = 25,imageY = 20;
int backImageX = 10, backImageY = 10;
int flag = 0;
for (int i = 0; i < [meetUPFrndPhotoArray count]; i++) {
flag ++;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = paths[0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:meetUPFrndPhotoArray[flag-1]];
UIImage *img1 = [UIImage imageWithContentsOfFile:savedImagePath];
if (!img1 || [UIImagePNGRepresentation(img1) length] <=0)
{
id path = meetUPFrndPhotoArray[flag-1];
path = [path stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
NSURL *url = [NSURL URLWithString:path];
NSMutableArray *arr = [[NSMutableArray alloc] initWithObjects:url, [NSString stringWithFormat:#"%d", flag], nil];
[self performSelectorInBackground:#selector(loadImageInBackground:) withObject:arr];
UIImageView *frndBackgroundImage = [[UIImageView alloc] initWithFrame:CGRectMake(backImageX, backImageY, 80, 80)];
frndBackgroundImage.image = [UIImage imageNamed:#"friend_image_background.png"];
frndBackgroundImage.backgroundColor = [UIColor clearColor];
frndBackgroundImage.layer.borderWidth = 2.0;
frndBackgroundImage.layer.masksToBounds = YES;
frndBackgroundImage.layer.shadowOffset = CGSizeMake(0, 1);
frndBackgroundImage.layer.shadowOpacity = 1;
frndBackgroundImage.layer.shadowRadius = 1.2;
frndBackgroundImage.layer.cornerRadius = 5.0;
frndBackgroundImage.layer.borderColor = [[UIColor clearColor] CGColor];
[friendsScrollView addSubview:frndBackgroundImage];
UIImageView *friendImage = [[UIImageView alloc] initWithFrame:CGRectMake(imageX, imageY, 50, 50)];
friendImage.tag = flag;
friendImage.image = [UIImage imageNamed:#"ic_user.png"];
[friendsScrollView addSubview:friendImage];
}
else
{
UIImageView *frndBackgroundImage = [[UIImageView alloc] initWithFrame:CGRectMake(backImageX, backImageY, 80, 80)];
frndBackgroundImage.image = [UIImage imageNamed:#"friend_image_background.png"];
frndBackgroundImage.backgroundColor = [UIColor clearColor];
frndBackgroundImage.layer.borderWidth = 2.0;
frndBackgroundImage.layer.masksToBounds = YES;
frndBackgroundImage.layer.shadowOffset = CGSizeMake(0, 1);
frndBackgroundImage.layer.shadowOpacity = 1;
frndBackgroundImage.layer.shadowRadius = 1.2;
frndBackgroundImage.layer.cornerRadius = 5.0;
frndBackgroundImage.layer.borderColor = [[UIColor clearColor] CGColor];
[friendsScrollView addSubview:frndBackgroundImage];
UIImageView *friendImage = [[UIImageView alloc] initWithFrame:CGRectMake(imageX, imageY, 50, 50)];
friendImage.tag = flag-1;
friendImage.image = img1;
[friendsScrollView addSubview:friendImage];
}
backImageX = backImageX + 80 + 10;
backImageY = 10;
imageX = imageX + 50 + 25 + 15;
imageY = 20;
}
Here meetUPFrndPhotoArray contains image URLs.
And
For Downloading images in Background -
- (void) loadImageInBackground:(NSArray *)urlAndTagReference{
NSData *imgData = [NSData dataWithContentsOfURL:urlAndTagReference[0]];
UIImage *img = [[UIImage alloc] initWithData:imgData];
NSMutableArray *arr = [[NSMutableArray alloc] initWithObjects:img, urlAndTagReference[1], nil];
[self performSelectorOnMainThread:#selector(assignImageToImageView:) withObject:arr waitUntilDone:YES];
}
For Assigning downloaded image to perticular imageView -
- (void) assignImageToImageView:(NSArray *)imgAndTagReference{
for (UIImageView *checkView in [friendsScrollView subviews] ){
if ([imgAndTagReference count] != 0) {
if ([checkView tag] == [imgAndTagReference[1] intValue]){
[checkView setImage:imgAndTagReference[0]];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = paths[0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:meetUPFrndPhotoArray[[imgAndTagReference[1] intValue]-1]];
UIImage* imageToSave = [checkView image];
NSData *imageData = UIImagePNGRepresentation(imageToSave);
[imageData writeToFile:savedImagePath atomically:NO];
}
}
}
}
Let me know if this helps you.Thanks in advance.All the best.
Make use of delegate functions, content size and content inset properties of the scrollview
Checkout this library It's a generic UIScrollView that reuses it's subviews like UITableView does (there is a dataSource object which is used to configure scroll view subviews)

iOS [__NSArrayM insertObject:atIndex:]: object cannot be nil - So Annoying

This is driving me crazy!
I'm seeing :
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
* First throw call stack:
(0x28f9012 0x271ee7e 0x28acb6a 0x28aca20 0x5866c 0x5eeee 0x154f103 0x154f42b 0x155cf80 0x1564fad 0x156589b 0x1565e93 0x1565a88 0x8628 0x2732705 0x146f2c0 0x16aba64 0x2732705 0x146f2c0 0x146f258 0x1530021 0x153057f 0x152f6e8 0x149ecef 0x149ef02 0x147cd4a 0x146e698 0x2d76df9 0x2d76ad0 0x286ebf5 0x286e962 0x289fbb6 0x289ef44 0x289ee1b 0x2d757e3 0x2d75668 0x146bffc 0x3210 0x3175)
libc++abi.dylib: terminate called throwing an exception
First break point is catching at :
NSMutableArray *imgArray = [NSMutableArray array];
IMReviewView *subview = [[IMReviewView alloc] initWithFrame:frame];
subview.delegate = self;
subview.photo = [[IMLocalUser localUser] cachedPhotoAtIndex:0];
UIImage *img1 = [[UIImage alloc] init];
img1 = subview.photo.image;
[imgArray addObject:img1]; //Breaking Here....
Not sure how this has suddenly started becoming an issue, i've restored back to earlier builds, tried another workstation however still seeing this error in the below code :
Any ideas? (had to trim the file hence the sudden ending)
#implementation IMReviewViewController {
UIScrollView *scrollView;
//UIPageControl *pageControl;
BOOL continueButtonDisabled;
NSUInteger downloadedImageCount;
}
#synthesize frameImage;
#synthesize removeButton;
#synthesize caption;
#synthesize delegate;
#synthesize photo = _photo;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)dealloc
{
NSLog(#"dealloc - IMReviewViewController");
}
- (void)buttonPressed:(id)sender
{
// if (self.delegate && [self.delegate respondsToSelector:#selector(removePhoto:)]) {
// [self.delegate removePhoto:removePhoto:self.photo];
// }
for (UIView *view in scrollView.subviews) {
[view removeFromSuperview];
}
[self.navigationController popViewControllerAnimated:YES];
}
- (void)drawRect:(CGRect)rect
{
// Drawing code
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetShadow(context, CGSizeMake(3.0f, 3.0f), 2.0f);
if (self.frameImage) {
[self.frameImage drawInRect:rect];
}
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)setReviewImages
{
continueButtonDisabled = YES;
downloadedImageCount = 0;
NSArray *reviewViews = scrollView.subviews;
for (IMReviewView *reviewView in reviewViews) {
[reviewView removeFromSuperview];
}
NSUInteger photoCount = [[IMLocalUser localUser] cachedPhotoCount];
if ( nPageNum == 0 ){
for (NSUInteger i = 0; i < photoCount; i++) {
CGRect frame;
frame.origin.x = scrollView.frame.size.width * i;
frame.origin.y = 65;
frame.size = CGSizeMake(scrollView.frame.size.width, 327.0f);
IMReviewView *subview = [[IMReviewView alloc] initWithFrame:frame];
subview.delegate = self;
subview.photo = [[IMLocalUser localUser] cachedPhotoAtIndex:i];
[scrollView addSubview:subview];
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * i, scrollView.frame.size.height);
UILabel *headingLabel = [[UILabel alloc] initWithFrame:CGRectMake(12, 20, 300, 30)];
[self.view addSubview:headingLabel];
headingLabel.text = #"Time To Preview!";
headingLabel.textColor = [UIColor blackColor];
headingLabel.textAlignment = UITextAlignmentCenter;
headingLabel.textAlignment = NSTextAlignmentCenter;
headingLabel.tag = 10;
headingLabel.backgroundColor = [UIColor clearColor];
headingLabel.font = [UIFont boldSystemFontOfSize:26.0f];
headingLabel.hidden = NO;
headingLabel.highlighted = YES;
headingLabel.highlightedTextColor = [UIColor blackColor];
headingLabel.lineBreakMode = YES;
headingLabel.numberOfLines = 0;
}
// pageControl.numberOfPages = photoCoun
}else if( nPageNum == 1 ){
int nVWidth, nVHeight;
nVWidth = self.view.frame.size.width;
nVHeight = (self.view.frame.size.height - 310) / 2;
CGRect frame;
frame.origin.x = 27.0f;
frame.origin.y = 65;
frame.size = CGSizeMake(269.5f, 253.5f);
IMReviewView *subview = [[IMReviewView alloc] initWithFrame:frame];
subview.delegate = self;
subview.photo = [[IMLocalUser localUser] cachedPhotoAtIndex:0];
UIImage *img1 = [[UIImage alloc] init];
img1 = subview.photo.image;
NSURL *imageURL = [[NSBundle mainBundle] URLForResource:#"FramedBack" withExtension:#"png"];
frameImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageURL]];
//Header Text above print
UILabel *headingLabel = [[UILabel alloc] initWithFrame:CGRectMake(12, 20, 300, 30)];
[self.view addSubview:headingLabel];
headingLabel.text = #"Time To Preview!";
headingLabel.textColor = [UIColor blackColor];
headingLabel.textAlignment = UITextAlignmentCenter;
headingLabel.textAlignment = NSTextAlignmentCenter;
headingLabel.tag = 10;
headingLabel.backgroundColor = [UIColor clearColor];
headingLabel.font = [UIFont boldSystemFontOfSize:26.0f];
headingLabel.hidden = NO;
headingLabel.highlighted = YES;
headingLabel.highlightedTextColor = [UIColor blackColor];
headingLabel.lineBreakMode = YES;
headingLabel.numberOfLines = 0;
//----------------
UIGraphicsBeginImageContext(CGSizeMake(IMAGE_WIDTH, IMAGE_HEIGHT));
CGSize divSize = CGSizeMake(IMAGE_WIDTH, IMAGE_HEIGHT);
UIImage *image = [[UIImage alloc] init];
image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageURL]];
//image = [image resizedImage:divSize interpolationQuality:kCGInterpolationDefault];
[image drawInRect:CGRectMake(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT)];
image = img1;
image = [image resizedImage:divSize interpolationQuality:kCGInterpolationDefault];
[image drawInRect:CGRectMake(300, 300, divSize.width-600, divSize.height-600)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView *viewImage = [[UIImageView alloc] initWithImage:newImage];
[viewImage setFrame:frame];
[scrollView addSubview:viewImage];
NSURL *buottonImageURL = [[NSBundle mainBundle] URLForResource:#"RemoveIcon#2x" withExtension:#"png"];
UIImage *buttonImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:buottonImageURL]];
self.removeButton = [[UIButton alloc] initWithFrame:CGRectMake(13.0f, 50, 38.0f, 38.0f)];
[self.removeButton setImage:buttonImage forState:UIControlStateNormal];
[self.removeButton addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[scrollView addSubview:self.removeButton];
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width, scrollView.frame.size.height);
}else if( nPageNum == 2 ){
int nVWidth, nVHeight;
nVWidth = self.view.frame.size.width;
nVHeight = (self.view.frame.size.height - 330) / 2;
CGRect frame;
frame.origin.x = 27.0f;
frame.origin.y = 65;
frame.size = CGSizeMake(269.5f, 253.5f);
NSMutableArray *imgArray = [NSMutableArray array];
IMReviewView *subview = [[IMReviewView alloc] initWithFrame:frame];
subview.delegate = self;
subview.photo = [[IMLocalUser localUser] cachedPhotoAtIndex:0];
UIImage *img1 = [[UIImage alloc] init];
img1 = subview.photo.image;
[imgArray addObject:img1];
subview.photo = [[IMLocalUser localUser] cachedPhotoAtIndex:1];
UIImage *img2 = [[UIImage alloc] init];
img2 = subview.photo.image;
[imgArray addObject:img2];
subview.photo = [[IMLocalUser localUser] cachedPhotoAtIndex:2];
UIImage *img3 = [[UIImage alloc] init];
img3 = subview.photo.image;
[imgArray addObject:img3];
subview.photo = [[IMLocalUser localUser] cachedPhotoAtIndex:3];
UIImage *img4 = [[UIImage alloc] init];
img4 = subview.photo.image;
[imgArray addObject:img4];
NSURL *imageURL = [[NSBundle mainBundle] URLForResource:#"FramedBack" withExtension:#"png"];
frameImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageURL]];
//Header Text above print
UILabel *headingLabel = [[UILabel alloc] initWithFrame:CGRectMake(12, 20, 300, 30)];
[self.view addSubview:headingLabel];
headingLabel.text = #"Time To Preview!";
headingLabel.textColor = [UIColor blackColor];
headingLabel.textAlignment = UITextAlignmentCenter;
headingLabel.textAlignment = NSTextAlignmentCenter;
headingLabel.tag = 10;
headingLabel.backgroundColor = [UIColor clearColor];
headingLabel.font = [UIFont boldSystemFontOfSize:26.0f];
headingLabel.hidden = NO;
headingLabel.highlighted = YES;
headingLabel.highlightedTextColor = [UIColor blackColor];
headingLabel.lineBreakMode = YES;
headingLabel.numberOfLines = 0;
//Footer Text under print
UILabel *footer = [[UILabel alloc] initWithFrame:CGRectMake(50, 330, 226, 50)];
[self.view addSubview:footer];
footer.text = #"*Please note, you cannot select the layout of the images within the grid at this time.";
footer.textColor = [UIColor blackColor];
footer.textAlignment = UITextAlignmentCenter;
footer.textAlignment = NSTextAlignmentCenter;
footer.tag = 10;
footer.backgroundColor = [UIColor clearColor];
footer.font = [UIFont systemFontOfSize:9.0f];
footer.hidden = NO;
footer.highlighted = YES;
footer.highlightedTextColor = [UIColor blackColor];
footer.lineBreakMode = YES;
footer.numberOfLines = 0;
//----------------
UIGraphicsBeginImageContext(CGSizeMake(IMAGE_WIDTH, IMAGE_HEIGHT));
CGSize divSize = CGSizeMake(IMAGE_WIDTH / 2, IMAGE_HEIGHT / 2);
UIImage *image = [[UIImage alloc] init];
image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageURL]];
image = [image resizedImage:divSize interpolationQuality:kCGInterpolationDefault];
[image drawInRect:CGRectMake(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT)];
image = [imgArray objectAtIndex:0];
image = [image resizedImage:divSize interpolationQuality:kCGInterpolationDefault];
[image drawInRect:CGRectMake(200, 200, divSize.width-210, divSize.height-210)];
image = [imgArray objectAtIndex:1];
image = [image resizedImage:divSize interpolationQuality:kCGInterpolationDefault];
[image drawInRect:CGRectMake(divSize.width, 200, divSize.width-210, divSize.height-210)];
image = [imgArray objectAtIndex:2];
image = [image resizedImage:divSize interpolationQuality:kCGInterpolationDefault];
[image drawInRect:CGRectMake(200, divSize.height, divSize.width-210, divSize.height-210)];
image = [imgArray objectAtIndex:3];
image = [image resizedImage:divSize interpolationQuality:kCGInterpolationDefault];
[image drawInRect:CGRectMake(divSize.width, divSize.height, divSize.width-210, divSize.height-210)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView *viewImage = [[UIImageView alloc] initWithImage:newImage];
[viewImage setFrame:frame];
[scrollView addSubview:viewImage];
NSURL *buottonImageURL = [[NSBundle mainBundle] URLForResource:#"RemoveIcon#2x" withExtension:#"png"];
UIImage *buttonImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:buottonImageURL]];
self.removeButton = [[UIButton alloc] initWithFrame:CGRectMake(13.0f, 50, 38.0f, 38.0f)];
[self.removeButton setImage:buttonImage forState:UIControlStateNormal];
[self.removeButton addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[scrollView addSubview:self.removeButton];
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width, scrollView.frame.size.height);
}else if( nPageNum == 3 ){
int nVWidth, nVHeight;
nVWidth = self.view.frame.size.width;
nVHeight = (self.view.frame.size.height - 243) / 2;
CGRect frame;
frame.origin.x = 27.0f;
frame.origin.y = 65;
frame.size = CGSizeMake(269.5f, 253.5f);
NSMutableArray *imgArray = [NSMutableArray array];
IMReviewView *subview = [[IMReviewView alloc] initWithFrame:frame];
subview.delegate = self;
subview.photo = [[IMLocalUser localUser] cachedPhotoAtIndex:0];
UIImage *img1 = [[UIImage alloc] init];
img1 = subview.photo.image;
[imgArray addObject:img1];
subview.photo = [[IMLocalUser localUser] cachedPhotoAtIndex:1];
UIImage *img2 = [[UIImage alloc] init];
img2 = subview.photo.image;
[imgArray addObject:img2];
subview.photo = [[IMLocalUser localUser] cachedPhotoAtIndex:2];
UIImage *img3 = [[UIImage alloc] init];
img3 = subview.photo.image;
[imgArray addObject:img3];
subview.photo = [[IMLocalUser localUser] cachedPhotoAtIndex:3];
UIImage *img4 = [[UIImage alloc] init];
img4 = subview.photo.image;
[imgArray addObject:img4];
subview.photo = [[IMLocalUser localUser] cachedPhotoAtIndex:4];
UIImage *img5 = [[UIImage alloc] init];
img5 = subview.photo.image;
[imgArray addObject:img5];
subview.photo = [[IMLocalUser localUser] cachedPhotoAtIndex:5];
UIImage *img6 = [[UIImage alloc] init];
img6 = subview.photo.image;
[imgArray addObject:img6];
subview.photo = [[IMLocalUser localUser] cachedPhotoAtIndex:6];
UIImage *img7 = [[UIImage alloc] init];
img7 = subview.photo.image;
[imgArray addObject:img7];
subview.photo = [[IMLocalUser localUser] cachedPhotoAtIndex:7];
UIImage *img8 = [[UIImage alloc] init];
img8 = subview.photo.image;
[imgArray addObject:img8];
subview.photo = [[IMLocalUser localUser] cachedPhotoAtIndex:8];
UIImage *img9 = [[UIImage alloc] init];
img9 = subview.photo.image;
[imgArray addObject:img9];
NSURL *imageURL = [[NSBundle mainBundle] URLForResource:#"FramedBack" withExtension:#"png"];
frameImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageURL]];
//Header Text above print
UILabel *headingLabel = [[UILabel alloc] initWithFrame:CGRectMake(12, 20, 300, 30)];
[self.view addSubview:headingLabel];
headingLabel.text = #"Time To Preview!";
headingLabel.textColor = [UIColor blackColor];
headingLabel.textAlignment = UITextAlignmentCenter;
headingLabel.textAlignment = NSTextAlignmentCenter;
headingLabel.tag = 10;
headingLabel.backgroundColor = [UIColor clearColor];
headingLabel.font = [UIFont boldSystemFontOfSize:26.0f];
headingLabel.hidden = NO;
headingLabel.highlighted = YES;
headingLabel.highlightedTextColor = [UIColor blackColor];
headingLabel.lineBreakMode = YES;
headingLabel.numberOfLines = 0;
//Footer Text under print
UILabel *footer = [[UILabel alloc] initWithFrame:CGRectMake(50, 330, 226, 50)];
[self.view addSubview:footer];
footer.text = #"*Please note, you cannot select the layout of the images within the grid at this time.";
footer.textColor = [UIColor blackColor];
footer.textAlignment = UITextAlignmentCenter;
footer.textAlignment = NSTextAlignmentCenter;
footer.tag = 10;
footer.backgroundColor = [UIColor clearColor];
footer.font = [UIFont systemFontOfSize:9.0f];
footer.hidden = NO;
footer.highlighted = YES;
footer.highlightedTextColor = [UIColor blackColor];
footer.lineBreakMode = YES;
footer.numberOfLines = 0;
//----------------
UIGraphicsBeginImageContext(CGSizeMake(IMAGE_WIDTH, IMAGE_HEIGHT));
CGSize divSize = CGSizeMake(IMAGE_WIDTH / 3, IMAGE_HEIGHT / 3);
UIImage *image = [[UIImage alloc] init];
image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageURL]];
image = [image resizedImage:divSize interpolationQuality:kCGInterpolationDefault];
[image drawInRect:CGRectMake(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT)];
image = [imgArray objectAtIndex:0];
image = [image resizedImage:divSize interpolationQuality:kCGInterpolationDefault];
[image drawInRect:CGRectMake(210, 210, divSize.width-150, divSize.height-150)];
image = [imgArray objectAtIndex:1];
image = [image resizedImage:divSize interpolationQuality:kCGInterpolationDefault];
[image drawInRect:CGRectMake(divSize.width + 70, 210, divSize.width-150, divSize.height-150)];
image = [imgArray objectAtIndex:2];
image = [image resizedImage:divSize interpolationQuality:kCGInterpolationDefault];
[image drawInRect:CGRectMake(divSize.width * 2 - 70, 210, divSize.width-150, divSize.height-150)];
image = [imgArray objectAtIndex:3];
image = [image resizedImage:divSize interpolationQuality:kCGInterpolationDefault];
[image drawInRect:CGRectMake(210, divSize.height + 70, divSize.width-150, divSize.height-150)];
image = [imgArray objectAtIndex:4];
image = [image resizedImage:divSize interpolationQuality:kCGInterpolationDefault];
[image drawInRect:CGRectMake(divSize.width + 70, divSize.height + 70, divSize.width-150, divSize.height-150)];
image = [imgArray objectAtIndex:5];
image = [image resizedImage:divSize interpolationQuality:kCGInterpolationDefault];
[image drawInRect:CGRectMake(divSize.width * 2 - 70, divSize.height + 70, divSize.width-150, divSize.height-150)];
image = [imgArray objectAtIndex:6];
image = [image resizedImage:divSize interpolationQuality:kCGInterpolationDefault];
[image drawInRect:CGRectMake(210, divSize.height*2 -70, divSize.width-150, divSize.height-150)];
image = [imgArray objectAtIndex:7];
image = [image resizedImage:divSize interpolationQuality:kCGInterpolationDefault];
[image drawInRect:CGRectMake(divSize.width + 70, divSize.height*2 -70, divSize.width-150, divSize.height-150)];
image = [imgArray objectAtIndex:8];
image = [image resizedImage:divSize interpolationQuality:kCGInterpolationDefault];
[image drawInRect:CGRectMake(divSize.width * 2 - 70, divSize.height*2 -70, divSize.width-150, divSize.height-150)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView *viewImage = [[UIImageView alloc] initWithImage:newImage];
[viewImage setFrame:frame];
[scrollView addSubview:viewImage];
NSURL *buottonImageURL = [[NSBundle mainBundle] URLForResource:#"RemoveIcon#2x" withExtension:#"png"];
UIImage *buttonImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:buottonImageURL]];
self.removeButton = [[UIButton alloc] initWithFrame:CGRectMake(13.0f, 50, 38.0f, 38.0f)];
[self.removeButton setImage:buttonImage forState:UIControlStateNormal];
[self.removeButton addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[scrollView addSubview:self.removeButton];
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width, scrollView.frame.size.height);
}else if( nPageNum == 4 ){
int nVWidth, nVHeight;
nVWidth = self.view.frame.size.width;
nVHeight = (self.view.frame.size.height - 243) / 2;
CGRect frame;
frame.origin.x = 27.0f;
// frame.origin.y = 23.0f;
frame.origin.y = 65;
frame.size = CGSizeMake(269.5f, 253.5f);
NSMutableArray *imgArray = [NSMutableArray array];
IMReviewView *subview = [[IMReviewView alloc] initWithFrame:frame];
subview.delegate = self;
subview.photo = [[IMLocalUser localUser] cachedPhotoAtIndex:0];
UIImage *img1 = [[UIImage alloc] init];
img1 = subview.photo.image;
[imgArray addObject:img1];
subview.photo = [[IMLocalUser localUser] cachedPhotoAtIndex:1];
UIImage *img2 = [[UIImage alloc] init];
img2 = subview.photo.image;
[imgArray addObject:img2];
subview.photo = [[IMLocalUser localUser] cachedPhotoAtIndex:2];
UIImage *img3 = [[UIImage alloc] init];
img3 = subview.photo.image;
[imgArray addObject:img3];
subview.photo = [[IMLocalUser localUser] cachedPhotoAtIndex:3];
UIImage *img4 = [[UIImage alloc] init];
img4 = subview.photo.image;
[imgArray addObject:img4];
subview.photo = [[IMLocalUser localUser] cachedPhotoAtIndex:4];
UIImage *img5 = [[UIImage alloc] init];
img5 = subview.photo.image;
[imgArray addObject:img5];
subview.photo = [[IMLocalUser localUser] cachedPhotoAtIndex:5];
UIImage *img6 = [[UIImage alloc] init];
img6 = subview.photo.image;
[imgArray addObject:img6];
subview.photo = [[IMLocalUser localUser] cachedPhotoAtIndex:6];
UIImage *img7 = [[UIImage alloc] init];
img7 = subview.photo.image;
[imgArray addObject:img7];
subview.photo = [[IMLocalUser localUser] cachedPhotoAtIndex:7];
UIImage *img8 = [[UIImage alloc] init];
img8 = subview.photo.image;
[imgArray addObject:img8];
subview.photo = [[IMLocalUser localUser] cachedPhotoAtIndex:8];
UIImage *img9 = [[UIImage alloc] init];
img9 = subview.photo.image;
[imgArray addObject:img9];
subview.photo = [[IMLocalUser localUser] cachedPhotoAtIndex:9];
UIImage *img10 = [[UIImage alloc] init];
img10 = subview.photo.image;
[imgArray addObject:img10];
subview.photo = [[IMLocalUser localUser] cachedPhotoAtIndex:10];
UIImage *img11 = [[UIImage alloc] init];
img11 = subview.photo.image;
[imgArray addObject:img11];
subview.photo = [[IMLocalUser localUser] cachedPhotoAtIndex:11];
UIImage *img12 = [[UIImage alloc] init];
img12 = subview.photo.image;
[imgArray addObject:img12];
subview.photo = [[IMLocalUser localUser] cachedPhotoAtIndex:12];
UIImage *img13 = [[UIImage alloc] init];
img13 = subview.photo.image;
[imgArray addObject:img13];
subview.photo = [[IMLocalUser localUser] cachedPhotoAtIndex:13];
UIImage *img14 = [[UIImage alloc] init];
img14 = subview.photo.image;
[imgArray addObject:img14];
subview.photo = [[IMLocalUser localUser] cachedPhotoAtIndex:14];
UIImage *img15 = [[UIImage alloc] init];
img15 = subview.photo.image;
[imgArray addObject:img15];
subview.photo = [[IMLocalUser localUser] cachedPhotoAtIndex:15];
UIImage *img16 = [[UIImage alloc] init];
img16 = subview.photo.image;
[imgArray addObject:img16];
Since the error is on the line:
[imgArray addObject:img1];
this means that img1 is nil. Since you get img1 from subview.photo.image this means one of three things:
subview.photo.image is nil.
subview.photo is nil.
subview is nil.
Since you just created subview then it probably isn't #3. Since you get subview.photo from:
[[IMLocalUser localUser] cachedPhotoAtIndex:0];
You should verify that this returns a non-nil value. If it is non-nil then verify the image property isn't nil.
Side note:
These two lines:
UIImage *img1 = [[UIImage alloc] init];
img1 = subview.photo.image;
create a wasted UIImage. Just do:
UIImage *img1 = subview.photo.image;

Loop add UIImageView Cocoa Touch

having a problem adding multiple ImageViews to my view. I have an array of images and what to cycle through them and add to my view. This snippet adds an image to view, but with no frame!
- (void)cycleImages:(NSArray *)images {
//create an image view
int fromLeft = 5;
int profilesCount = [images count] - 1;
for(int n = 0; n <= profilesCount; n++){
//add image view to view
[self.view addSubview:[[[UIImageView alloc] initWithFrame:CGRectMake(5, fromLeft, 48, 48)] initWithImage:[[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[images objectAtIndex:n]]]]]];
fromLeft = fromLeft + 53;
NSLog(#"%d", fromLeft);
}
}
Thanks for your help!
Dex
Its hard to read such code and you have mem leaks. Please, try this.
- (void)cycleImages:(NSArray *)images
{
int fromLeft = 5;
NSURL* url = nil;
UIImage* img = nil;
UIImageView* imgView = nil;
NSData* imgData = nil;
for (NSString* imgURLStr in images)//Using enumeration much more easy and in ObjC style
{
//Put breakpoint here and check all varibles initialized coorecly step by step
url = [NSURL URLWithString:imgURLStr];
imgData = [NSData dataWithContentsOfURL:url];
img = [[UIImage alloc] initWithData:imgData];
imgView = [[UIImageView alloc] initWithImage:img];
imgView.frame = CGRectMake(5, fromLeft, 48, 48);
[self.view addSubView:imgView];
fromLeft = fromLeft + 53;
NSLog(#"%d", fromLeft);
//Clean up
[img release];
[imgView release];
}
}
Thanks!

Resources