I am creating a NC Widget and can't seem to adjust the height of the widget.
Here is my code:
-(UIView *)view {
if (_view == nil)
{
CGRect frame = CGRectMake(2,0,316,191); // Added
_view = [[[UIView alloc] initWithFrame:(frame)] autorelease]; //(2., 0., 316., 71.)]; <--ORIGINAL VALUE
// Setting frame Height dont work... :/
UIImage *bg = [[UIImage imageWithContentsOfFile:#"/System/Library/WeeAppPlugins/SMSWidget.bundle/Background.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:71];
UIImageView *bgView = [[UIImageView alloc] initWithImage:bg];
bgView.frame = CGRectMake(0, 0, 316, 191);
[_view addSubview:bgView];
[bgView release];
}
This is an example of how you can do that :
float myWidgetHeigth = 500 ; // The desired height
_view = [[UIView alloc] initWithFrame:CGRectMake(2, 0, 316, myWidgetHeigth)];
UIImage *bg = [[UIImage imageWithContentsOfFile:#"/System/Library/WeeAppPlugins/WorldClockNC.bundle/WeeAppBackground.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(5, 5, 5, 5)];
UIImageView bgview = [[UIImageView alloc] initWithImage:bg];
bgview.frame = CGRectMake(0, 0, 316, myWidgetHeigth);
[_view addSubview:bgview];
If you are developing a plugin for the notification Center, refer to the BBWeeAppController-Protocol.h you will need also to implement this method : "- (float)viewHeight".
Also, I have seen from your code you are using theses methods "stretchableImageWithLeftCapWidth:5 topCapHeight:71", that are deprecated in IOS 5.0 --> https://developer.apple.com/library/ios/#documentation/uikit/reference/UIImage_Class/DeprecationAppendix/AppendixADeprecatedAPI.html#//apple_ref/occ/instm/UIImage/stretchableImageWithLeftCapWidth:topCapHeight: So, use the method "resizableImageWithCapInsets" ;-).
I'm not a pro on it, I'm learning ObjC, so if you have somes questions, let me know.
Related
I have find that for most photos in the gallery, [ALAsset thumbnail] will return the thumbnail with an inward black translucent border.
My question is, how can I obtain the thumbnail without this border?
You have a lot of options. If you just need to display it on screen you could simply fake it so that 1 pixel of the thumbnail isn't visible. You can put a UIImageView inside a UIView that clips to bounds.
UIView* view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
view.backgroundColor = [UIColor clearColor];
view.clipsToBounds = YES;
UIImageView* imgView = [[UIImageView alloc] initWithFrame:CGRectMake(-1, -1, 202, 202)];
imgView.image = [asset thumbnail];
[view addSubview:imgView];
Or better yet, make a UIView subclass and override drawRect.
-(void)drawRect:(CGRect)rect
{
UIImage* thumb = [asset thumbnail];
[thumb drawInRect:CGRectMake(rect.origin.x-1, rect.origin.y-1, rect.size.width+2, rect.size.height+2)];
}
Or you can use the aspectRatioThumbnail instead and make it squared yourself.
UIImageView* imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
imgView.image = [asset aspectRatioThumbnail];
imgView.contentMode = UIViewContentModeScaleAspectFill;
Or if you actually need to crop the UIImage itself for some reason you can do this.
UIImage* thumb = [asset thumbnail];
CGRect cropRect = CGRectMake(1, 1, thumb.size.width-2, thumb.size.height-2);
cropRect = CGRectMake(cropRect.origin.x*thumb.scale, cropRect.origin.y*thumb.scale, cropRect.size.height*cropRect.scale);
CGImageRef imageRef = CGImageCreateWithImageInRect([thumb CGImage], cropRect);
UIImage* result = [UIImage imageWithCGImage:imageRef scale:thumb.scale orientation:thumb.imageOrientation];
CGImageRelease(imageRef);
There are no methods to get thumbnail without 1 pixel black border.
You can use also
[asset aspectRatioThumbnail]; // but it is not rounded.
So i think you should resize the image yourself using:
asset.defaultRepresentation.fullScreenImage or
asset.defaultRepresentation.fullResolutionImage
I've got a MKMap, on top of this I have a little legend. The labels & images inside the legend successfully show on top of the map, but the rectangle/bezier itself doesn't show.
I've tried adding it as a subview and that fails. Is there a way around this?
I need touch to still be enabled for the map.
Thanks.
- (void) drawRect:(CGRect)rect
{
//TODO
//Hover view for touching icons
//Get rect on top of map as well
//Border on frame
//IsIpad slight larger frame/text/image
int conX = [GeneralHelper GetCenteredXPosForObj:220 :self];
int conY = 60;
//Create box
CGRect container = CGRectMake(conX, conY, 220, 50);
UIBezierPath* path = [UIBezierPath bezierPathWithRoundedRect:container cornerRadius:5.0];
[[UIColor blueColor] setFill];
[path fillWithBlendMode:kCGBlendModeNormal alpha:0.7];
//How to add to subview..?
//NZPost image 38x37
UIImageView *nzPostImageView = [[UIImageView alloc] initWithFrame:CGRectMake(conX + 20, conY+15, 25, 24)];
[nzPostImageView setImage:[UIImage imageNamed:#"icon-map-post.png"]];
[self addSubview:nzPostImageView];
//NZPost label
LegendLabel *postLabel = [[LegendLabel alloc] initWithFrame:CGRectMake(conX + 50, 78, conY, 15) ];
postLabel.text = #"NZ post";
[self addSubview: postLabel];
//Incharge image 38x38
UIImageView *inChargeImageView = [[UIImageView alloc] initWithFrame:CGRectMake(conX + 120, conY+15, 25, 24)];
[inChargeImageView setImage:[UIImage imageNamed:#"icon-map-incharge.png"]];
[self addSubview:inChargeImageView];
//Incharge label
LegendLabel *inChargeLabel = [[LegendLabel alloc] initWithFrame:CGRectMake(conX + 150, conY+18, 60, 15) ];
inChargeLabel.text = #"In charge";
[self addSubview: inChargeLabel];
}
I am trying to add set of imageviews on a UIScrollView. My problem is that the imageviews are overlapping with each other even though I increase the x value. What's also weird is that this problem is only for devices with ios 4.2.1, but works on simulator 4.2 and other device ios.
Here is my code:
- (void)addScrollView {
[self setImages];
scrollView = [[UIScrollView alloc] initWithFrame:self.frame];
scrollView.delegate = self;
[scrollView setBackgroundColor:[UIColor blackColor]];
[scrollView setCanCancelContentTouches:NO];
scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
scrollView.clipsToBounds = YES;
scrollView.scrollEnabled = YES;
scrollView.pagingEnabled = YES;
[self addSubview:scrollView];
NSUInteger nimages;
CGFloat cx = 0;
for (nimages = 1; nimages < [imagesArray count] ; nimages++) {
UIImage *image = [UIImage imageNamed:[imagesArray objectAtIndex:nimages]];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
CGRect rect = imageView.frame;
rect.origin.x = cx;
rect.origin.y = 0;
rect.size.width = 320;
rect.size.height = 480;
imageView.frame = rect;
[scrollView addSubview:imageView];
[imageView release];
cx += rect.size.width;
}
[scrollView setContentSize:CGSizeMake(cx, [scrollView bounds].size.height)];
}
Called from here:
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self addScrollView];
UIImage *handImg;
UIImageView *longhand = [[UIImageView alloc] init];
minuteHand = [[UIView alloc] initWithFrame:CGRectMake(0, 55, 320, 480)];
handImg = [UIImage imageNamed:#"clock_long_hand.png"];
longhand.image = handImg;
/************** TRY COMMENT OUT HERE **************/
longhand.frame = CGRectMake(155 - (handImg.size.width / 2), 240 - handImg.size.height, handImg.size.width, handImg.size.height);
[minuteHand addSubview:longhand];
[self addSubview:minuteHand];
/************** END COMMENT OUT HERE **************/
[longhand release];
}
return self;
}
UPDATE:
This code is in a UIView subclass which is why I use [self...] instead of [self.view...].
I tried experimenting and found that when I comment out the indicated parts, the scrollview works fine. But when it is included, this is when the imageviews are overlapped.
I know that the scrollview's content size is correct because its indicator continues to scroll, with just a plain black background.
I am really confused why this works on some ios and not on others. Hope you can help.
This might have something to do with autoresizing mask. The width actual images, I mean .png files is bigger than 320, right?
I'm wanting to know if there's a way I can transform my view to look something like iPhone folders. In other words, I want my view to split somewhere in the middle and reveal a view underneath it. Is this possible?
EDIT:
Per the suggestion below, I could take a screenshot of my application by doing this:
UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Not sure what to do with this, however.
EDIT:2
I've figured out how to add some shadows to my view, and here's what I've achieved (cropped to show relevant part):
EDIT:3
http://github.com/jwilling/JWFolders
the basic thought will be to take a picture of your current state and split it somewhere. Then animate both parts by setting a new frame. I don't know how to take a screenshot programmatically so I can't provide sample codeā¦
EDIT: hey hey it's not looking great but it works ^^
// wouldn't be sharp on retina displays, instead use "withOptions" and set scale to 0.0
// UIGraphicsBeginImageContext(self.view.bounds.size);
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, 0.0);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *f = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGRect fstRect = CGRectMake(0, 0, 320, 200);
CGRect sndRect = CGRectMake(0, 200, 320, 260); // was 0,200,320,280
CGImageRef fImageRef = CGImageCreateWithImageInRect([f CGImage], fstRect);
UIImage *fCroppedImage = [UIImage imageWithCGImage:fImageRef];
CGImageRelease(fImageRef);
CGImageRef sImageRef = CGImageCreateWithImageInRect([f CGImage], sndRect);
UIImage *sCroppedImage = [UIImage imageWithCGImage:sImageRef];
CGImageRelease(sImageRef);
UIImageView *first = [[UIImageView alloc]initWithFrame:fstRect];
first.image = fCroppedImage;
//first.contentMode = UIViewContentModeTop;
UIImageView *second = [[UIImageView alloc]initWithFrame:sndRect];
second.image = sCroppedImage;
//second.contentMode = UIViewContentModeBottom;
UIView *blank = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 460)];
blank.backgroundColor = [UIColor darkGrayColor];
[self.view addSubview:blank];
[self.view addSubview:first];
[self.view addSubview:second];
[UIView animateWithDuration:2.0 animations:^{
second.center = CGPointMake(second.center.x, second.center.y+75);
}];
You can uncomment the two .contentMode lines and the quality will improve but in my case the subview has an offset of 10px or so (you can see it by setting a background color to both subviews)
//EDIT 2: ok found that bug. Had used the whole 320x480 screen, but had to cut off the status bar so it should be 320x460 and all is working great ;)
Instead of taking a snapshot of the view, you could use a separate view for each row of icons. You'll have to do a bit more work with repositioning stuff, but the rows won't be static when the folder is open (in other words, they'll keep redrawing as necessary).
I took relikd's code as a base and made it a bit more dynamic.
You can specify split position and direction when calling the function and I added a boarder to the split images.
#define splitAnimationTime 0.5
- (void)split:(SplitDirection)splitDirection
atYPostition:(int)splitYPosition
withRevealedViewHeight:(int)revealedViewHeight{
// wouldn't be sharp on retina displays, instead use "withOptions" and set scale to 0.0
// UIGraphicsBeginImageContext(self.view.bounds.size);
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, 0.0);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *f = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGRect fullScreenRect = [self getScreenFrameForCurrentOrientation];
CGRect upperSplitRect = CGRectMake(0, 0,fullScreenRect.size.width, splitYPosition);
CGRect lowerSplitRect = CGRectMake(0, splitYPosition, fullScreenRect.size.width, fullScreenRect.size.height-splitYPosition);
CGImageRef upperImageRef = CGImageCreateWithImageInRect([f CGImage], upperSplitRect);
UIImage *upperCroppedImage = [UIImage imageWithCGImage:upperImageRef];
CGImageRelease(upperImageRef);
CGImageRef lowerImageRef = CGImageCreateWithImageInRect([f CGImage], lowerSplitRect);
UIImage *lowerCroppedImage = [UIImage imageWithCGImage:lowerImageRef];
CGImageRelease(lowerImageRef);
UIImageView *upperImage = [[UIImageView alloc]initWithFrame:upperSplitRect];
upperImage.image = upperCroppedImage;
//first.contentMode = UIViewContentModeTop;
UIView *upperBoarder = [[UIView alloc]initWithFrame:CGRectMake(0, splitYPosition, fullScreenRect.size.width, 1)];
upperBoarder.backgroundColor = [UIColor whiteColor];
[upperImage addSubview:upperBoarder];
UIImageView *lowerImage = [[UIImageView alloc]initWithFrame:lowerSplitRect];
lowerImage.image = lowerCroppedImage;
//second.contentMode = UIViewContentModeBottom;
UIView *lowerBoarder = [[UIView alloc]initWithFrame:CGRectMake(0, 0, fullScreenRect.size.width, 1)];
lowerBoarder.backgroundColor = [UIColor whiteColor];
[lowerImage addSubview:lowerBoarder];
int reveledViewYPosition = splitYPosition;
if(splitDirection==SplitDirectionUp){
reveledViewYPosition = splitYPosition - revealedViewHeight;
}
UIView *revealedView = [[UIView alloc]initWithFrame:CGRectMake(0, reveledViewYPosition, fullScreenRect.size.width, revealedViewHeight)];
revealedView.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
[self.view addSubview:revealedView];
[self.view addSubview:upperImage];
[self.view addSubview:lowerImage];
[UIView animateWithDuration:splitAnimationTime animations:^{
if(splitDirection==SplitDirectionUp){
upperImage.center = CGPointMake(upperImage.center.x, upperImage.center.y-revealedViewHeight);
} else { //assume down
lowerImage.center = CGPointMake(lowerImage.center.x, lowerImage.center.y+revealedViewHeight);
}
}];
}
This means I can call it like this:
[self split:SplitDirectionUp atYPostition:500 withRevealedViewHeight:200];
I used these conveniance functions in the updated split function:
- (CGRect)getScreenFrameForCurrentOrientation {
return [self getScreenFrameForOrientation:[UIApplication sharedApplication].statusBarOrientation];
}
- (CGRect)getScreenFrameForOrientation:(UIInterfaceOrientation)orientation {
UIScreen *screen = [UIScreen mainScreen];
CGRect fullScreenRect = screen.bounds;
BOOL statusBarHidden = [UIApplication sharedApplication].statusBarHidden;
//implicitly in Portrait orientation.
if(orientation == UIInterfaceOrientationLandscapeRight || orientation == UIInterfaceOrientationLandscapeLeft){
CGRect temp = CGRectZero;
temp.size.width = fullScreenRect.size.height;
temp.size.height = fullScreenRect.size.width;
fullScreenRect = temp;
}
if(!statusBarHidden){
CGFloat statusBarHeight = 20;
fullScreenRect.size.height -= statusBarHeight;
}
return fullScreenRect;
}
and this enum:
typedef enum SplitDirection
{
SplitDirectionDown,
SplitDirectionUp
}SplitDirection;
Adding a return to normaal function and adding the arrow would be a great addition.
i have following problem: i generate subviews UIView in an UIScrollView including UIImageViews.
NSArray *bilder = [[NSArray alloc] initWithObjects:#"lol.jpg", #"lol2.jpg", nil];
NSArray *added = [[NSMutableArray alloc] init];
UIImageView *tmpView;
for (NSString *name in bilder) {
UIImage *image = [UIImage imageNamed:name];
tmpView = [[UIImageView alloc] initWithImage:image];
tmpView.contentMode = UIViewContentModeScaleAspectFit;
tmpView.frame = CGRectMake(0, 0, 300, 300);
[added addObject:tmpView];
[tmpView release];
}
CGSize framy = mainFrame.frame.size;
NSUInteger x = 0;
for (UIView *view in added) {
[mainFrame addSubview:view];
view.frame = CGRectMake(framy.height * x++, 0, framy.height, framy.width);
}
mainFrame.scrollEnabled = YES;
mainFrame.pagingEnabled = YES;
mainFrame.contentSize = CGSizeMake(framy.height * [added count], framy.width);
mainFrame.backgroundColor = [UIColor blackColor];
i get image file names out of an array. now i want to resize the imageview to a specific size, but it won't work. any advice?
thanks + regards
UIImage lacks the setSize: method of NSImage, but this post seems to add a scaleToSize: method using a category on UIImage.
Change tmpView.frame to use a new CGRect:
tmpView.frame = CGRectMake(tmpView.frame.origin.x,
tmpView.frame.origin.y,
newWidth,
newHeight);
What I see is:
view.frame = CGRectMake(framy.height * x++, 0, framy.height, framy.width);
But if you look at CGRectMake...
Declaration: CGRect CGRectMake (
CGFloat x,
CGFloat y,
CGFloat width,
CGFloat height
);
You are setting the X value, not the Y value. I expect, based on your later call to:
mainFrame.contentSize = CGSizeMake(framy.height * [added count], framy.width);
that you intend to make the these images appear on top of each other, not side by side.
Suggest:
view.frame = CGRectMake(framy.height * x++, 0, framy.height, framy.width);
to
view.frame = CGRectMake(0,framy.height * x++, framy.height, framy.width);