I am very new to xcode and I am playing around with image arrays and NSMutableArray. When I run the code below it just print out the names of the image for example "image1.png". Any tips to fix this problem would be appreciated. Thanks.
- (NSMutableArray*)restaurantArray;
{
if (_restaurantArray == nil) {
_restaurantArray = [[NSMutableArray alloc] initWithObjects:
#"image1.png",
#"image2.png",
#"image3.png",
nil];
}
return _restaurantArray;
}
-(NSString*) randomRestaurant
{
int random = arc4random_uniform(self.restaurantArray.count);
return [self.restaurantArray objectAtIndex:random];
}
You could do it in this way also.
- (NSMutableArray*)restaurantArray;
{
if (_restaurantArray == nil) {
_restaurantArray = [[NSMutableArray alloc] initWithObjects:[UIImage imageNamed:#"image1.png"],
[UIImage imageNamed:#"image2.png"],
[UIImage imageNamed:#"image3.png"],
nil];
}
return _restaurantArray;
}
Do you mean this?:
NSString *imageName = [self randomRestaurant];
UIImage *image = [UIImage imageName:imageName];
If you want to display the image then do this:
Suppose you have an imageview name imgView,
imgView.image = [UIImage imageNamed:[self randomRestaurant]];
Or, if you just want the image, then call this:
UIImage *img = [UIImage imageNamed:[self randomRestaurant]];
In you given code, you are just returning the name of the image, not the actual image. If you want an array of image then you can do it by this way:
-(NSMutableArray*) restaurantArray;
{
if (_restaurantArray == nil) {
UIImage *img = [UIImage imageNamed:#"image1.png"];
UIImage *img1 = [UIImage imageNamed:#"image2.png"];
UIImage *img2 = [UIImage imageNamed:#"image3.png"];
_restaurantArray = [[NSMutableArray alloc] initWithObjects:
img,
img1,
img2,
nil];
}
return _restaurantArray;
}
-(UIImage*) randomRestaurant
{
int random = arc4random_uniform(self.restaurantArray.count);
return [self.restaurantArray objectAtIndex:random];
}
Hope this helps.. :)
Related
Earlier i used SDWebimage third party to display images, now i removed that 3rd party due some other reason. is there any other way to Display GIF image from URL in Imageview without using third party...
Well you can do it simply by following next steps:
Add the following functions
#import <ImageIO/ImageIO.h>
#import <MobileCoreServices/MobileCoreServices.h>
UIImage *GIFFromData(NSData *data) {
if (!data) {
return nil;
}
CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)(data), NULL);
UIImage *image = nil;
if (GIFSourceContainsAnimatedGIF(source)) {
image = GIFFromImageSource(source);
} else {
image = [UIImage imageWithData:data];
}
if (source) {
CFRelease(source);
}
return image;
}
BOOL GIFSourceContainsAnimatedGIF(CGImageSourceRef source) {
return (source && UTTypeConformsTo(CGImageSourceGetType(source), kUTTypeGIF) && CGImageSourceGetCount(source) > 1);
}
UIImage *GIFFromImageSource(CGImageSourceRef source) {
CFRetain(source);
NSUInteger numberOfFrames = CGImageSourceGetCount(source);
NSMutableArray<UIImage *> *images = [NSMutableArray arrayWithCapacity:numberOfFrames];
NSTimeInterval duration = 0.0;
for (NSUInteger i = 0; i < numberOfFrames; ++i) {
CGImageRef image = CGImageSourceCreateImageAtIndex(source, i, NULL);
if (image) {
UIImage *frameImage = [UIImage imageWithCGImage:image scale:1.0 orientation:UIImageOrientationUp];
[images addObject:frameImage];
CFRelease(image);
} else {
continue;
}
duration += GIFSourceGetFrameDelay(source, i);
}
CFRelease(source);
return [UIImage animatedImageWithImages:images duration:duration];
}
NSTimeInterval GIFSourceGetFrameDelay(CGImageSourceRef source, NSUInteger index) {
NSTimeInterval frameDelay = 0;
CFDictionaryRef imageProperties = CGImageSourceCopyPropertiesAtIndex(source, index, NULL);
if (!imageProperties) {
return frameDelay;
}
CFDictionaryRef gifProperties = nil;
if (CFDictionaryGetValueIfPresent(imageProperties, kCGImagePropertyGIFDictionary, (const void **)&gifProperties)) {
const void *durationValue = nil;
if (CFDictionaryGetValueIfPresent(gifProperties, kCGImagePropertyGIFUnclampedDelayTime, &durationValue)) {
frameDelay = [(__bridge NSNumber *)durationValue doubleValue];
if (frameDelay <= 0) {
if (CFDictionaryGetValueIfPresent(gifProperties, kCGImagePropertyGIFDelayTime, &durationValue)) {
frameDelay = [(__bridge NSNumber *)durationValue doubleValue];
}
}
}
}
CFRelease(imageProperties);
return frameDelay;
}
Download your GIF with NSURLSession
Display it
UIImage *image = GIFFromData(data);
UIImageView *view = [[UIImageView alloc] initWithImage:image];
Hope this helps :)
UIImageView* animatedImageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
animatedImageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"image1.gif"],
[UIImage imageNamed:#"image2.gif"],
[UIImage imageNamed:#"image3.gif"],
[UIImage imageNamed:#"image4.gif"], nil];
animatedImageView.animationDuration = 1.0f;
animatedImageView.animationRepeatCount = 0;
[animatedImageView startAnimating];
[self.view addSubview: animatedImageView];
Add animated Gif image in Iphone UIImageView
I want to put defaults images if i don't have images from url feed.
if(![[stories objectAtIndex:storyIndex] valueForKey:#"url"]==nil)
{ NSNumber *numberOfKeyInstances = [stories valueForKey:#"key"];
imageArray=[NSArray arrayWithObjects:[UIImage imageNamed:#"1.png"],nil];
int randomNumber_ = rand ()%19;
UIImage *image = [imageArray objectAtIndex:indexPath.row];
cell.imag=image;
}
else{
if (![[[stories objectAtIndex:storyIndex] valueForKey:#"url"] isEqualToString:#""]) {
[cell.imag setImageWithURL:[NSURL URLWithString:[[stories objectAtIndex:storyIndex] objectForKey:#"url"]] placeholderImage:[UIImage imageNamed:#"Alb.png"]];
}
}
This is my code, but i get [UIImage setContentMode:]: unrecognized selector sent to instance 0x7f908c1dcce0'
How can i solved?
Firstly, imageArray is initialised with only one image. So calling [imageArray objectAtIndex:indexPath.row] is a problem. You dont need an image array for that. Secondly, the argument inside the first if statement doesn't look right.
Also, as #midhun-mp Pointed out, u should be probably using cell.imag.image.
Try this :
if([[stories objectAtIndex:storyIndex] valueForKey:#"url"] == nil)
{
cell.imag.image = [UIImage imageNamed:#"1.png"];
}
else if ([[[stories objectAtIndex:storyIndex] valueForKey:#"url"] isEqualToString:#""])
{
cell.imag.image = [UIImage imageNamed:#"1.png"];
}
else
{
//SET YOUR IMAGE HERE
}
Try to change
UIImage *image = [imageArray objectAtIndex:indexPath.row];
to
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:[imageArray objectAtIndex:indexPath.row]]];
I suspect that cell.imag is a UIImageView not UIImage property.
So you need to change:
cell.imag=image;
to
cell.imag.image = image;
I call the following method to get an image which I display in a UIImageView. When I am done with the UIImageView I release myImageView.image. I am happy with how I do this but Instruments is showing a leak of UIImage. How should I be releasing the below instance of UIImage?
-(UIImage *)getImageWithRef:(NSString *)ref {
UIImage *myImage = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:#"foobar_%#",ref]];
if (myImage == nil) {
myImage = [[UIImage alloc] initWithContentsOfFile:#"foobar_defaultImage"];
}
return myImage;
}
Thanks in advance
Try using auto-release when you return your image.
-(UIImage *)getImageWithRef:(NSString *)ref {
UIImage *myImage = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:#"foobar_%#",ref]];
if (myImage == nil) {
myImage = [[UIImage alloc] initWithContentsOfFile:#"foobar_defaultImage"];
}
return [myImage autorelease];
}
I have a UIImageView on my storyboard, which is linked to an outlet in the code. When I run the code, it only shows the static picture that the storyboard shows, not the animation that I create programmatically. Any ideas?
for(int i=0; i<4; i++) {
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:#"loading%d",i]];
[self.images addObject:image];
}
self.loadingView.animationImages = self.images;
self.loadingView.animationDuration = 2;
self.loadingView.animationRepeatCount = 1;
[self.loadingView startAnimating];
Note: loadingView is the UIImageView, images is an NSMutableArray.
If you want to make run time array you can use like this way.
NSMutableArray *images = [[NSMutableArray alloc] init];
for (int i = 1 ; i < 4; i++) {
[images addObject:[UIImage imageNamed:[NSString stringWithFormat: #"loading%d.jpg", i]]];
}
self.loadingView.animationImages = images;
self.loadingView.animationDuration = 2;
self.loadingView.animationRepeatCount = 1;
[self.loadingView startAnimating];
Hope this will help you. It's working fine for me.
I recommend you to use array directly like this.
NSArray *images = [[NSArray alloc] initWithObjects:
[UIImage imageNamed:#"1.jpg"],
[UIImage imageNamed:#"2.jpg"],
nil];
self.loadingView.animationImages = images;
self.loadingView.animationDuration = 2;
self.loadingView.animationRepeatCount = 1;
[self.loadingView startAnimating];
I have made an app, but on ipad 1 it crashes in the end part. On ipad-higher it remains working, but still I think I am doing something wrong, I really need to make something working more economically. But what is it?
In the end part it all happens. Before that, several screens are put onto each other and in the simulator when you go back they all are dismissed. But on the ipad 1 you will not even reach the end of the end part. I will try to explain what happens and several idea's about what is happening I have.
You can swipe on the screen and retina images (big images) are exchanged with the four corners that can get selected, each with a different image-background. There are six different scenes, so there are 6x4 + 6x1 for-normal-0-situation, 25 big retina images. There are only 5 frames working on top of each other (4 corners and 1 background), but I set inactive stuff to nil and load active images with imageNamed. It goes well on the simulator, but it crashes on the ipad 1, with no low memory warning or what so ever.
I've studied improvements: a) autorelease pool b) imageNamed replaced by imageContentsOfFile.
The autorelease pools don't have any effect. imageNamed, I couldn't get imageContentsOfFile working, so I skipped the operation.
Any ideas of what is so excessively wrong and what kind of thoughts can be applied to have it work more economically, for it somewhere is demanding way too much in the end.
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint tappedPt = [[touches anyObject] locationInView:self.view];
NSArray *subviews = [[NSArray alloc]init];
subviews = [self.view subviews];
// Return if there are no subviews
if ([subviews count] == 0) return;
for (UIView *subview in subviews) {
if ([subview pointInside:[self.view convertPoint:tappedPt toView:subview] withEvent:event]){
[self checkAndChange:subview];
[self modifyChoice:subview];
}
}
}
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
// Get the subviews of the view
CGPoint tappedPt = [[touches anyObject] locationInView:self.view];
NSArray *subviews = [[NSArray alloc]init];
subviews = [self.view subviews];
// Return if there are no subviews
if ([subviews count] == 0) return;
for (UIView *subview in subviews) {
if ([subview pointInside:[self.view convertPoint:tappedPt toView:subview] withEvent:event]){
[self checkAndChange:subview];
[self modifyChoice:subview];
}
}
}
-(void) modifyChoice:(UIView *)subview
{
if(subview==area1LinksBoven){[gekozenExpressieBijDimensie replaceObjectAtIndex:dimensie-1 withObject:[NSNumber numberWithInteger:1]];}
if(subview==area2RechtsBoven){[gekozenExpressieBijDimensie replaceObjectAtIndex:dimensie-1 withObject:[NSNumber numberWithInteger:2]];}
if(subview==area3RechtsOnder){[gekozenExpressieBijDimensie replaceObjectAtIndex:dimensie-1 withObject:[NSNumber numberWithInteger:3]];}
if(subview==area4LinksOnder){[gekozenExpressieBijDimensie replaceObjectAtIndex:dimensie-1 withObject:[NSNumber numberWithInteger:4]];}
if(maxChoice<dimensie){maxChoice=dimensie;
if(maxChoice==1){headerGevuld.image = [UIImage imageNamed:#"TOP1.png"];}
if(maxChoice==2){headerGevuld.image = [UIImage imageNamed:#"TOP2.png"];}
if(maxChoice==3){headerGevuld.image = [UIImage imageNamed:#"TOP3.png"];}
if(maxChoice==4){headerGevuld.image = [UIImage imageNamed:#"TOP4.png"];}
if(maxChoice==5){headerGevuld.image = [UIImage imageNamed:#"TOP5.png"];}
if(maxChoice==6){headerGevuld.image = [UIImage imageNamed:#"TOP6.png"];}
}
}
-(void) checkAndChange:(UIView *)subview{
if(subview==area1LinksBoven){
area1LinksBoven.image = [UIImage imageNamed:#"SELECTION1.png"];
area2RechtsBoven.image = nil;
area3RechtsOnder.image = nil;
area4LinksOnder.image = nil;
footer.image = [UIImage imageNamed:#"FOOT1.png"];
#autoreleasepool {
if(dimensie==1){
filmAchtergrond0.image = [UIImage imageNamed:#"1PassiefActiefOntspanning.jpg"];}
if(dimensie==2){
filmAchtergrond0.image = [UIImage imageNamed:#"1MakkelijkMoeilijkMaster.jpg"];}
if(dimensie==3){
filmAchtergrond0.image = [UIImage imageNamed:#"1VeiligGevaarlijkGeborgenheid.jpg"];}
if(dimensie==4){filmAchtergrond0.image = [UIImage imageNamed:#"1NormaalBijzonderGeaard.jpg"];}
if(dimensie==5){filmAchtergrond0.image = [UIImage imageNamed:#"1AlleenSamenUniek.jpg"];}
if(dimensie==6){filmAchtergrond0.image = [UIImage imageNamed:#"1OrdeChaosDuidelijk.jpg"];}
}
[arrowRight setImage:[UIImage imageNamed:#"ARROW_RIGHT_ACTIVE.png"] forState:UIControlStateNormal];
}
if(subview==area2RechtsBoven){
area1LinksBoven.image = nil;
area2RechtsBoven.image = [UIImage imageNamed:#"SELECTION2.png"];
area3RechtsOnder.image = nil;
area4LinksOnder.image = nil;
footer.image = [UIImage imageNamed:#"FOOT2.png"];
#autoreleasepool {
if(dimensie==1){filmAchtergrond0.image = [UIImage imageNamed:#"2PassiefActiefEnergiek.jpg"];}
if(dimensie==2){
filmAchtergrond0.image = [UIImage imageNamed:#"2MakkelijkMoeilijkUitdagend.jpg"];}
if(dimensie==3){
filmAchtergrond0.image = [UIImage imageNamed:#"2VeiligGevaarlijkSpannend.jpg"];}
if(dimensie==4){filmAchtergrond0.image = [UIImage imageNamed:#"2NormaalBijzonderWow.jpg"];}
if(dimensie==5){filmAchtergrond0.image = [UIImage imageNamed:#"2AlleenSamenGezellig.jpg"];}
if(dimensie==6){filmAchtergrond0.image = [UIImage imageNamed:#"2OrdeChaosImproviseren.jpg"];}
}
[arrowRight setImage:[UIImage imageNamed:#"ARROW_RIGHT_ACTIVE.png"] forState:UIControlStateNormal];
}
if(subview==area3RechtsOnder){
area1LinksBoven.image = nil;
area2RechtsBoven.image = nil;
area3RechtsOnder.image = [UIImage imageNamed:#"SELECTION3.png"];
area4LinksOnder.image = nil;
footer.image = [UIImage imageNamed:#"FOOT3.png"];
#autoreleasepool {
if(dimensie==1){
filmAchtergrond0.image = [UIImage imageNamed:#"3PassiefActiefUitputting.jpg"];}
if(dimensie==2){
filmAchtergrond0.image = [UIImage imageNamed:#"3MakkelijkMoeilijkFrustrerend.jpg"];}
if(dimensie==3){
filmAchtergrond0.image = [UIImage imageNamed:#"3VeiligGevaarlijkGevaarlijk.jpg"];}
if(dimensie==4){
filmAchtergrond0.image = [UIImage imageNamed:#"3NormaalBijzonderOnbegrijjpelijk.jpg"];}
if(dimensie==5){filmAchtergrond0.image = [UIImage imageNamed:#"3AlleenSamenGroepsdruk.jpg"];}
if(dimensie==6){filmAchtergrond0.image = [UIImage imageNamed:#"3OrdeChaosRommeltje.jpg"];}
}
[arrowRight setImage:[UIImage imageNamed:#"ARROW_RIGHT_ACTIVE.png"] forState:UIControlStateNormal];
}
if(subview==area4LinksOnder){
area1LinksBoven.image = nil;
area2RechtsBoven.image = nil;
area3RechtsOnder.image = nil;
area4LinksOnder.image = [UIImage imageNamed:#"SELECTION4.png"];
footer.image = [UIImage imageNamed:#"FOOT4.png"];
#autoreleasepool {
if(dimensie==1){filmAchtergrond0.image = [UIImage imageNamed:#"4PassiefActiefApathie.jpg"];}
if(dimensie==2){filmAchtergrond0.image = [UIImage imageNamed:#"4MakkelijkMoeilijkSaai.jpg"];}
if(dimensie==3){
filmAchtergrond0.image = [UIImage imageNamed:#"4VeiligGevaarlijkBeklemmend.jpg"];}
if(dimensie==4){filmAchtergrond0.image = [UIImage imageNamed:#"4NormaalBijzonderSleur.jpg"];}
if(dimensie==5){filmAchtergrond0.image = [UIImage imageNamed:#"4AlleenSamenEenzaam.jpg"];}
if(dimensie==6){filmAchtergrond0.image = [UIImage imageNamed:#"4OrdeChaosRigide.jpg"];}
}
[arrowRight setImage:[UIImage imageNamed:#"ARROW_RIGHT_ACTIVE.png"] forState:UIControlStateNormal];
}
if(dimensie==1){headerPositie.image = [UIImage imageNamed:#"OUT1.png"];}
if(dimensie==2){headerPositie.image = [UIImage imageNamed:#"OUT2.png"];}
if(dimensie==3){headerPositie.image = [UIImage imageNamed:#"OUT3.png"];}
if(dimensie==4){headerPositie.image = [UIImage imageNamed:#"OUT4.png"];}
if(dimensie==5){headerPositie.image = [UIImage imageNamed:#"OUT5.png"];}
if(dimensie==6){headerPositie.image = [UIImage imageNamed:#"OUT6.png"];}
}
Load the images using UIImage imageWithContentsOfFile:
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"fileName" ofType:#"fileExtension"];
UIImage *img = [UIImage imageWithContentsOfFile:filePath];
See here the difference between imageNamed and imageWithContentsOfFile:
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIImage_Class/Reference/Reference.html