How to make images in WKInterfacePicker Transparent? - ios

I have a WKInterfacePicker which is set to Sequence mode. When i add images to it during runtime and display it on the apple watch, the images seems to have a black background (instead of having the transparent pixels). The Images have transparent pixels in them. Here is the code i used in willActivate function:
NSMutableArray *pItems = [[NSMutableArray alloc] initWithCapacity:31];
for(int i=0; i<30; ++i){
WKPickerItem *item = [[WKPickerItem alloc] init];
WKImage *img = nil;
if(i < 10)
img = [WKImage imageWithImageName:[NSString stringWithFormat:#"safeDial_0%d", i]];
else
img = [WKImage imageWithImageName:[NSString stringWithFormat:#"safeDial_%d", i]];
[item setContentImage:img];
[pItems addObject:item];
}
[self.pickerSafeLock setItems:[pItems copy]];
[self.pickerSafeLock setSelectedItemIndex:0];
[self.pickerSafeLock focus];
Note: pickerSafeLock is the WKInterfacePicker
Also, when i use the same images on a WKImage, i see that the transparent pixels work fine.
Is there a way to make the Images in WKInterfacePicker transparent?

After trying multiple ways (including #DaGaMs's solution), i found an considerably tricky solution to the problem. I have used a WKInterfacePicker and an associated WKInterfaceImage alongside it. Here is a way to overcome the issue:
In the Storyboard of your WatchKit App add a WKinterfaceImage, WKInterfaceGroup and WKInterfacePicker inside a WKInterfaceGroup.
Here is how it would look:
This is just to hide the Picker offscreen, so that we can fake the animation from the Picker to the WKInterfaceImage.
Once you do this, on the IBAction of WKInterfacePicker set the corresponding images like this:
- (IBAction)pickerAction:(NSInteger)value {
[self.yourWKInteraceImage setImage:imgPickerList[value]];
}
Note: imgPickerList contains all the images that you want to animate (same as the Picker).

Related

Should I use GIF file or animation code on iOS?

I intend to make a splash screen on iOS consists of a person, a chair objects. Each of these objects has seperated aspects like the hands, head, body and feets that animated together. I wonder which is better way to go? Importing a GIF file or coding CALayer objects then adding animation?
You can't use GIF files or any code for animation for your launchscreen (splash screen), you can only use a static image, a PNG or JPG (if the launchscreen is a storyboard).
So, if you want your app's startup with some animation then you should manage it in your first view controller.
You can animate imageview with image sets like,
UIImageView* myImageViewForAnimation = [[UIImageView alloc] initWithFrame:self.view.bounds];
myImageViewForAnimation.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"image1"],
[UIImage imageNamed:#"image2"],
[UIImage imageNamed:#"image3"],
[UIImage imageNamed:#"image4"], nil];
myImageViewForAnimation.animationDuration = 1.0f;
myImageViewForAnimation.animationRepeatCount = 0;
[myImageViewForAnimation startAnimating];
[self.view addSubview: myImageViewForAnimation];
Update (as asked in comment) :
You can get your view on which you have added gesture recognizer in your action method or you can set tag to every imageview and handle it in action method.
For example,
-(void)tapOnProfileImage : (UITapGestureRecognizer*)recog{
UIImageView *tempView = (UIImageView *)recog.view;
// or
if (recog.view.tag == 1) {
// image 1
}
if (recog.view.tag == 2) {
//image 2
}
}
So, on every image view, you should add target with selector - tapOnProfileImage and you can differentiate it as mentioned in above code snippet!
You can use gif image but not directly on launch xib. You create one view controller and load gif image on that view controller, once animation is completed then remove this controller from navigation stack. https://github.com/Flipboard/FLAnimatedImage use this gif image view control.

iOS Image Viewer: Set Zoomed Image Back when Scrolling

I have a gallery view that I use to, surprise, view images. :) I wanted it to be very similar to the standard iOS photo app with the same basic functionality: scrolling through images that are zoomable. To do this, I use a regular scroll view which I then populate with other scroll views that contain the images. The basic idea is something like this:
And, I do the populating the following way:
for (int i = 0; i < [imageGalleryArray count]; i++)
{
UIScrollView *imageContainer = [[UIScrollView alloc] initWithFrame:CGRectMake((windowSize.width * i), 0.0f, windowSize.width, windowSize.height)];
imageContainer.contentSize = CGSizeMake(windowSize.width, windowSize.height);
imageContainer.showsHorizontalScrollIndicator = NO;
imageContainer.showsVerticalScrollIndicator = NO;
imageContainer.bouncesZoom = YES;
imageContainer.delegate = self;
imageContainer.minimumZoomScale = 1.0f;
imageContainer.maximumZoomScale = 3.0f;
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, windowSize.width, windowSize.height)];
imageView.image = [[imageGalleryArray objectAtIndex:i] objectForKey:#"image"];
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.tag = IMAGEZOOMVIEW;
imageView.opaque = YES;
[imageContainer addSubview:imageView];
[galleryScrollView addSubview:imageContainer];
// Add it to array for proper removal later
[contentScrollViewSubviewList addObject:imageContainer];
}
Quite simple and it works fine. The only problem arises when I am zoomed into an image and want to scroll to another. The expected behavior in this case would be to set the zoom level of all images back to their original size. I do this in the scrollViewDidEndDecelerating delegate method like so:
if (scrollView.tag == GALLERYSCROLLVIEW)
{
for (int i = 0; i < [contentScrollViewSubviewList count]; i++)
{
[[contentScrollViewSubviewList objectAtIndex:i] setZoomScale:1.0f animated:YES];
}
}
Unfortunately, it does not work (or rather not as intended). I did some poking around and found out that the image does indeed animate back to its original size when it is the active slide. To elaborate: if I zoom into an image and scroll to another, the image stays zoomed in. When I scroll back to it, that is when it zooms back to its original size. I, however, need the image to scale back as soon as I move away from it ().
just like in the iOS image viewer).
Frankly, I do not understand the whole zoom mechanic enough to find out how to do this properly, and the UIScrollView documentation is of no help either. Could someone show me how to solve my problem?

How can i set images after generating imageview in ios?

Suppose I generated the 100 image views. First of all I wish to generate all 100 image views and add them in my scroll view.
So image view are clearly display it’s in position using background color
Then after I want to set images into the image view. I have to types of Dictionary (Which comes from the another class). One Dictionary into set Already Downloaded images and another one into only generate object (Remaining download images). I am downloading this image Asynchronously.
Then how can I set images into the image view?
-(void)GenerateImageView{
for (int ivalue = 0; ivalue < 100; ivalue++) {
imageView = [[UIImageView alloc] init];
//Give tag to the image view
imageView.tag = ivalue;
[imageView setBackgroundColor:[UIColor whiteColor]];
CGFloat xOrigin =ivalue *ascrollView.frame.size.width+50;
imageView.frame=CGRectMake(xOrigin40,imgYpos,ascrollView.frame.size.width-20, scrollViewHight);
//set imageview into the scroll view
[ascrollView addSubview:imageView];
}
}
Then how can I set images into the imageview?
UIImageView *imageView = [ascrollView viewWithTag:some_tag];
if ([imageView isKindOfClass:[UIImageView Class]])
imageView.image = your_UIImage_object;
u can not access the directly image view.And also make sure ur tag not start with 0.bcs 0 is a by default main view tag
imageView = (UIImageView *)[ascrollView viewWithTag:some_tag];
imageView.image = your_UIImage_object;
You could use the code Avt suggested, but instead of reinventing the wheel, I would suggest to look into
AFNetworking
an open-source networking lib. It comes with an extension to the UIImageView which allows you to load the images asynchronously out-of-the-box. There are many other useful features too.
After importing the extension you could then use:
[imageView setImageWithURL:[NSURL URLWithString:#"yourImageURL"]];
the image gets loaded asynchronously and automaticly set when it's done loading.

How to create an image with black and white pixels?

I'm trying to create a black and white image that on the one hand can be displayed in an UIImageView and on the other hand it should be possible to share this image via e-mail or just be saved to camera roll.
I've got a two dimensional array (NSArray containing other NSArrays) which contains a matrix with the NSInteger values 0 and 1 (for a white and a black pixel).
So I just want to place a black pixel for a 1 and a white one for a 0.
All other questions I found deal with changing pixels from an existing image.
I hope you can help me!
[EDIT]:
Of course, I didn't want someone to do my work. I couldn't figure out how to create a new image and place the pixels as I wanted to at first, so I tried to edit an existing image that only consists of white pixels and change the color of the pixel if necessary. So my code for trying this, but as you can see I had no idea how the change the pixel. I hope that shows that i was trying on my own.
- (UIImage*) createQRCodeWithText:(NSString*) text andErrorCorrectionLevel:(NSInteger) level {
QRGenerator *qr = [[QRGenerator alloc] init];
NSArray *matrix = [qr createQRCodeMatrixWithText:text andCorrectionLevel:level];
UIImage *image_base = [UIImage imageNamed:#"qr_base.png"];
CGImageRef imageRef = image_base.CGImage;
for (int row = 0; row < [matrix count]; row++) {
for (int column = 0; column < [matrix count]; column++) {
if ([[[matrix objectAtIndex:row] objectAtIndex:column] integerValue] == 1) {
//set pixel (column, row) black
}
}
}
return [[UIImage alloc] initWithCGImage:imageRef];
}
I would create the image from scratch using a CGBitmapContext. You can call CGBitmapContextCreate() to allocate the memory for the image. You can then walk through the pixels just like you're doing now, setting them from your array. When you've finished, you can call CGBitmapContextCreateImage() to make a CGImageRef out of it. If you need a UIImage you can call [+UIImage imageWithCGImage:] and pass it the CGImageRef you created above.

Managed array of UIView using some method

Hello I am noob in Xcode and I want to ask
I have array of UIView's with one image
NSInteger i;
for (i=0; i<CARD_AMOUNT; i++) {
[cardArray addObject:[UIImage imageNamed:#"Back.png"]];
}
and I want to draw it and then to do different things on each (move...etc)
or I must create 10 variables of UIImageView?
Could use a bit more explanation of the app you're trying to build, but essentially if you're wanting to display many different cards on one view (let's say, a game of solitaire) - you'll want to create many UIImageViews. Well, from an pure design perspective, you'd probably want a different class called CardView which knew how to render itself
So, if you wanted many different views all with one image, something like this:
for(int a=0; a<CARD_AMOUNT; a++) {
UIImageView *view = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Back.png"]];
[cardArray addObject:view];
// and maybe you'd want to set the frame locations, or add them to your current view as subviews, or ..
}
To address the comments, your custom CardView might have something like this (there's ways to optimize, but the general concept isn't too bad):
- (void)setRank: (int)rank andSuit:(int)suit {
self.image = [UIImage imageNamed:[NSString stringWithFormat:#"card-%d-%d.png",rank,suit];
self.rank = rank; self.suit = suit;
}
or, better yet (since a card's rank won't change) - some initWithRank: andSuit: method.. dunno.
There's probably 10 other ways to design this.

Resources