Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 01-25-2012, 01:04 PM   #1 (permalink)
Registered Member
 
Join Date: Jan 2009
Posts: 321
melmoup is on a distinguished road
Default create "negative" rounded rect

Hi,
I would like to create a rect with Bezier Path that is transparent inside but it keep colored opaque angles.
Basically it should be the negation of a rounded rect, I'll use it as a view to put on top of a rect UIImage view to make it look rounded.
Thanks
melmoup is offline   Reply With Quote
Old 01-25-2012, 01:18 PM   #2 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by melmoup View Post
Hi,
I would like to create a rect with Bezier Path that is transparent inside but it keep colored opaque angles.
Basically it should be the negation of a rounded rect, I'll use it as a view to put on top of a rect UIImage view to make it look rounded.
Thanks
It sounds like you want a mask that will hide the outer part of an image, but reveal a rounded rectangle shape on the inside.

There will be problems with that. The mask you create will cover not just the image view, but everything else on your window, which won't look right.

You would be better off to create a layer object, set it to the bounds you want, set it with rounded corners at the radius you want, and then install that layer as the mask layer of your image view. That will mask away everything but the rounded rectangle shape defined by the layer, without obscuring the other contents of your window.

Something like this:

Say I have an image view anImageView and I want to create a mask that's inset by 20 pixels on all sides and has rounded corners:

Code:
CGRect roundedRect = anImageView.bounds;
roundedRect = CGRectinset(roundedRect, 20, 20);
CALayer mask = [CALayer layer];
mask.bounds = imageRect;
mask.backgroundColor = [[UIColor whiteColor] CGColor]; //any opaque color works
mask.cornerRadius = 20;  //Adjust to taste
anImageView.layer.mask = mask;
That should do it. I did not compile the code above, much less test it. It might have minor typos or other mistakes in it. It's intended as a guide, not copy-and-paste-ready code.

EDIT: Note that there is special type of layer, a CAShapeLayer, that uses a CGPath to define the shape of the layer. You can use that to describe any shape that you can create with a CGPath. A Bezier path is essentially a UIKit wrapper around a CGPath; a bezier path has a CGPath property that lets you get the underlying CGPath from a bezier path, and you can use that as the path object for a CAShapeLayer. You can make a CAShapeLayer the mask for a layer. This is really, really powerful.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.

Last edited by Duncan C; 01-25-2012 at 01:22 PM.
Duncan C is offline   Reply With Quote
Old 01-25-2012, 01:50 PM   #3 (permalink)
Registered Member
 
Join Date: Jan 2009
Posts: 321
melmoup is on a distinguished road
Default

Thanks, I'll try that.
Actually I'm trying to avoid using layer in there, I tried on a UIImageView in a custom table cell and the following code slowed down scrolling (at least on a old iPhone 3G)

Code:
CALayer * l = [itemPicture layer];
[l setMasksToBounds:YES];
[l setCornerRadius:10.0];
melmoup is offline   Reply With Quote
Old 01-25-2012, 02:18 PM   #4 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by melmoup View Post
Thanks, I'll try that.
Actually I'm trying to avoid using layer in there, I tried on a UIImageView in a custom table cell and the following code slowed down scrolling (at least on a old iPhone 3G)

Code:
CALayer * l = [itemPicture layer];
[l setMasksToBounds:YES];
[l setCornerRadius:10.0];
The iPhone 3G is a dinosaur in terms of it's graphics hardware. It does not have much in the way of hardware graphics acceleration.

You can't extrapolate about performance on the 3G to determine the performance on newer hardware.

I recommend dropping the 3G and the old 2nd gen iPod touch from your supported products. It makes life much easier.


Note that you could quite easily use the code that I posted to create a cropped version of your image, cropped to your rounded rectangle, and then use that directly.

All you'd have to do is to create an offscreen context sized for the cropped image, render the cropped layer into the context, and then ask for an image from the context. It's about 4 more lines of code.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 01-25-2012, 03:42 PM   #5 (permalink)
Registered Member
 
Join Date: Jan 2009
Posts: 321
melmoup is on a distinguished road
Default

yep, but now it's really a challenge to make it as fast as possible.
Somewhere I was suggested that the primary problem with the approach with layer and corner radius is that it forces an offscreen render of the contents, which is very slow (especially when you have a lot of them).
So my idea was keep my image in a UIImageView, taking advantage of a UIImageView category for fetching the jpgs asyncronously that set as the imageview image when completed.
So always having a View on top could work.

Otherwise I could draw the UIImage in the drawRect clipping to a mask.
melmoup is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 391
11 members and 380 guests
Atatator, condor304, FrankWeller, glenn_sayers, iphonedevshani, MAMN84, mraalex, PowerGoofy, QuantumDoja, tim0504, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,674
Threads: 94,123
Posts: 402,908
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Atatator
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 05:53 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0