Re: Caching Streaming video

2011-06-28 Thread Eric Wing
 I am trying to playback video in my iOS app while I am loading and caching
 it at the same time. I fetch the video using a NSURLConnection and then
 store it in a local file, I start video playback of the local video file
 after a certain number of bytes are received. I have it working great in
 the simulator, I can start playing the video before I have received all of
 it, but when I go to run my app on my iPodTouch, I can only seem to play
 up to the number of bytes I had already received before I started
 playback. I can only play the entire video if I wait until I have receive
 the entire file before I start playback. I can also get the video to play
 completely if I stop the failed attempt with a [video stop] message and
 then start playing it again, pausing the video only doesn't work.

 Has anybody got this working, is it possible.


I haven't done this, but I would be interested in hearing about the
solution you come up with. What you describe sounds like a bug worth
filing a bug report on. I'll speculate the device implementation sees
that you are opening a file and uses the number of bytes as a hint to
the length of the video (incorrectly).

So a random, not completely thought out idea...what if you created an
intermediate local socket which you use to further separate the
temporary file from the NSFileHandle. Maybe the implementation won't
be able to find the file size if your NSFileHandle wraps a socket
instead.

-Eric
-- 
Beginning iPhone Games Development
http://playcontrol.net/iphonegamebook/
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


dispatch_async Performance Issues

2011-06-28 Thread Andreas Grosam
Hi All

I've implemented a typical consumer/producer pattern, using a container holding 
a FIFO list of buffers. This buffer list has functions to store and retrieve 
these buffers concurrently. Consumer and Producer are running in the global 
concurrent dispatch queue.

I'm experiencing huge differences in performance in two almost identical 
versions of the code. I've tried to figure the cause with Instruments, but 
didn't find any concrete hints. The xxx_block_invoke  of the consumer part of 
one version just takes considerable more time than the other.




The code basically works as follows:

The producer is simply creating a certain amount of buffers (NSData objects), 
initializing its content and storing them into the concurrent accessible buffer 
list. The producer part of the code is identical in both versions. The 
concurrently accessible buffers list is the same, too.

In the consumer part , the code simply retrieves buffers and processes them. 
The difference is in the processing of the received buffer content - that is, 
how the bytes of the buffer's content are accessed.
This tiny bit of difference in code should't make a huge difference in runtime, 
but it actually there is a huge difference, where ever it comes from!

The first uses a direct implementation using pointers accessing the content 
of the buffer.
The second implementation uses a C++ iterator concept. But when compiled with 
-03, it should produce almost the same code as the former. So, I would expect 
only minor differences in performance.

However, the difference is about factor 3! I have no idea what the cause of 
this huge difference is. The main part of the code is shown below. The source 
is actual C++ and uses just the dispatch library. Not every piece of source is 
shown, but I can provide it if necessary.


Additional Info:

Synchronizing the access is achieved using dispatch_semaphore objects. Storing 
a buffer into the buffers  list may block if the buffers list has reached its 
maximum number of buffers. Retrieving a buffer may block if there is no buffer 
available.

After testing, it seems, the implementation is correct.

Class CFDataConstBufferschar is the type of the buffer list. Is has two 
principal functions:
consume() and produce() which can be called concurrently.

consume() returns the next buffer in the list (FIFO). It may block until the 
buffer list has one available.

produce() puts back a buffer. It may block, if the buffer's capacity (max 
number of buffers) is reached.


Class CFDataConstBufferchar, the buffer class, is basically a thin wrapper 
around a CFDataRef.

Class semaphore is a thin wrapper around a dispatch_semahore_t.



Below are two functions whose runtime duration is measured. Note that the 
consumer part of the function ConcurrentProduceConsume() is written such that 
it mimics the code produced by the compiler in the second function 
ConcurrentProduceConsumeIter() which uses C++ Iterators - hence, it looks a bit 
more complex than necessary. The code for the iterator isn't shown here, though.

The buffer size was set from 4KB to 32KB, incrementing in steps.
The buffers' list capacity (max number of hold buffers) was set to 1 to 8.
LLVM compiler, Xcode 4.02.

For no apparent reason ConcurrentProduceConsume() performs significantly faster 
(about 2.5x) than ConcurrentProduceConsumeIter().

Is there a hidden cost for C++ instances in a block, for C++ exception 
handlers, etc.?


Thanks for tips!



// Create a buffers instance with capacity C, then produce N buffers with 
// BUFFER_SIZE bytes and fill them. Concurrently consume the buffers and 
// iterate over the consumed buffer.
// Performs concurrently on two threads. 
//
void ConcurrentProduceConsume(size_t C = 1, size_t N = 100)
{
typedef std::pairCFDataConstBufferchar, bool result_t;

// Create a buffers instance with at max C buffers:
CFDataConstBufferschar buffers(C);
CFDataConstBufferschar* buffersPtr = buffers;

const size_t TOTAL = BUFFER_SIZE * N;

// Get the global concurrent queue:
dispatch_queue_t queue = 
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

// Create a group in order to sync the two threads:
dispatch_group_t group = dispatch_group_create();

dispatch_group_async(group, queue,
^{
unsigned long k = 0;
UInt8 data[BUFFER_SIZE];
for (int i = 0; i  N; ++i) {
// fill the buffer:
for (int j = 0; j  BUFFER_SIZE; ++j, ++k) {
data[j] = char(k); 
} 
CFDataRef d = CFDataCreate(NULL, data, sizeof(data));
CFDataConstBufferchar buffer = d;
CFRelease(d);
buffersPtr-produce(buffer);
}

// put EOF:
buffersPtr-produce(CFDataConstBufferchar());
});


dispatch_group_async(group, queue,
^{
const 

sorting a pair of arrays

2011-06-28 Thread Rick C.
Hi,

I have a pair of arrays (one of integers and one of strings) and I need to sort 
the one of integers from largest to smallest and then however it gets sorted I 
need to copy that sorting in my strings array since they are related.  Any 
advice on what the best way to approach this is would be great since I don't 
have a lot of experience in sorting like this.  Thanks!

rc___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: sorting a pair of arrays

2011-06-28 Thread Mike Abdullah
You want an object that holds both integer and string as properties. Put those 
objects in an array and sort that.

On 28 Jun 2011, at 13:52, Rick C. wrote:

 Hi,
 
 I have a pair of arrays (one of integers and one of strings) and I need to 
 sort the one of integers from largest to smallest and then however it gets 
 sorted I need to copy that sorting in my strings array since they are 
 related.  Any advice on what the best way to approach this is would be great 
 since I don't have a lot of experience in sorting like this.  Thanks!
 
 rc___
 
 Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
 
 Please do not post admin requests or moderator comments to the list.
 Contact the moderators at cocoa-dev-admins(at)lists.apple.com
 
 Help/Unsubscribe/Update your Subscription:
 http://lists.apple.com/mailman/options/cocoa-dev/cocoadev%40mikeabdullah.net
 
 This email sent to cocoa...@mikeabdullah.net

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: sorting a pair of arrays

2011-06-28 Thread Mikkel Islay
Hi Rick,

It sounds to me like one way of looking at your data structures is as a 
NSDictionary with the first array as keys and the second as objects (values).
If so, you could then sort the keys using - (NSArray 
*)keysSortedByValueUsingComparator:(NSComparator)cmptr , or one of the other 
sorting methods, and use the returned array to access the objects in turn. If 
the array-contaner is important for your code add the sorted objects to an 
array.

NSArray, of course, also has a set of sorting methods, you could leverage to 
co-sort the strings in place.

Mikkel

On 28 Jun 2011, at 14:52, Rick C. wrote:

 Hi,
 
 I have a pair of arrays (one of integers and one of strings) and I need to 
 sort the one of integers from largest to smallest and then however it gets 
 sorted I need to copy that sorting in my strings array since they are 
 related.  Any advice on what the best way to approach this is would be great 
 since I don't have a lot of experience in sorting like this.  Thanks!
 
 rc___
 
 Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
 
 Please do not post admin requests or moderator comments to the list.
 Contact the moderators at cocoa-dev-admins(at)lists.apple.com
 
 Help/Unsubscribe/Update your Subscription:
 http://lists.apple.com/mailman/options/cocoa-dev/my.inputstream%40googlemail.com
 
 This email sent to my.inputstr...@googlemail.com
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Reading Adobe Photoshop header, Obj-C

2011-06-28 Thread Kevin Muldoon
Was writing a program to create thumbnails using SIPS, GhostScript,  
ImageMagick. During testing I found I needed to read the header file  
of Adobe Photoshop native documents directly because Duotone image  
color mode simply wasn't returning the proper color mode (in a variety  
of programs).


So I threw together this bit of code which works great for the first  
tag, but


// References

//http://www.adobe.com/devnet-apps/photoshop/fileformatashtml/PhotoshopFileFormats.htm#50577409_pgfId-1055726

//http://www.adobe.com/content/dam/Adobe/en/devnet/photoshop/psir/ps_image_resources.pdf

	NSFileHandle *fileHandler = [NSFileHandle  
fileHandleForReadingAtPath:@/Users/kevin/Desktop/balloon.psd];


	NSData *psdFileHeader = [NSData dataWithData:[fileHandler  
readDataOfLength:26]]; // read entire PSD header


NSData *signature = [psdFileHeader subdataWithRange: NSMakeRange(0,4)];
NSData *version = [psdFileHeader subdataWithRange: NSMakeRange(4,2)];
NSData *reserved = [psdFileHeader subdataWithRange: NSMakeRange(6,6)];

	NSLog(@Raw string is '%s' (length %d)\n, [signature bytes],  
[signature length]);
	NSLog(@Raw string is '%s' (length %d)\n, [version bytes], [version  
length]);
	NSLog(@Raw string is '%s' (length %d)\n, [reserved bytes],  
[reserved length]);


As you can see from the output, the 'version'  'reserved' isn't being  
read at all. Must I coerce/parse something here? Much thanks...


[Session started at 2011-06-28 09:14:31 -0400.]
2011-06-28 09:14:31.458 Read Standard Input[65929:10b] Raw string is  
'8BPS' (length 4)
2011-06-28 09:14:31.460 Read Standard Input[65929:10b] Raw string is  
'' (length 2)
2011-06-28 09:14:31.460 Read Standard Input[65929:10b] Raw string is  
'' (length 6)


Kevin Muldoon
e: caoimgh...@gmail.com

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: dispatch_async Performance Issues

2011-06-28 Thread Jonathan Taylor
Hi Andreas,

If I understand your post correctly, you are saying that you see a performance 
drop of 3x when using an iterator in your inner loop as opposed to using 
hand-written C code to do the iterating. Unfortunately you say you haven't 
actually posted the code relating to the iterator... but it seems to me that 
this is very likely to be the source of your problems!

Your title states dispatch_async Performance Issues, but have you actually 
looked into whether you see the same problem in a single-threaded case that 
does not use dispatch_async?

All I can suggest is that you examine the preprocessor output and even the 
generated assembly code with the aim of spotting any significant differences in 
the generated code in each case. It may well be that a function call is being 
generated as part of the iterator code, or something like that. Shark may help 
you pin down where the problem is, but you will probably need to have some 
level of appreciation of assembly code to be able to fully interpret the 
results for your optimized build. Unfortunately without the crucial part of 
your source code there's not a lot anyone else can do to help you in that 
respect...

Hope that helps a bit
Jonny___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: dispatch_async Performance Issues

2011-06-28 Thread Jonathan Taylor
... and more importantly I do not believe your code makes correct use of 
dispatch_group_async. Your first call adds a producer block to the global 
concurrent queue, which may start executing immediately. At some time soon 
afterwards you add a consumer block to the global concurrent queue, which again 
may start executing immediately - while the producer block is still in the 
process of producing. As far as I can see the code appears to have a number 
of major issues:

1. Theoretically your producer block may not have produced anything by the time 
your consumer block executes

2. What happens if your consumer block has processed all the currently-added 
blocks before the producer block has added the eof marker?

3. You have only one single consumer block, which means you will only have a 
single consumer thread running under Grand Central Dispatch, which rather 
defeats the purpose of using it at all! I think you may not have fully 
understood how GCD is intended to be used...

4. Are you sure that your mysterious and rather complicated-seeming 
CFDataConstBuffers class is sufficiently threadsafe for your producer to be 
adding to it at the same time as your consumer block is iterating through it? I 
am 99% sure that the explicit consumer code you wrote in 
ConcurrentProduceConsume is not threadsafe.

Jonny.___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Assigning to property with 'readonly' atribute not allowed

2011-06-28 Thread Fritz Anderson
On 27 Jun 2011, at 9:52 PM, Fernando Aureliano wrote:

 - (AQGridViewCell *) gridView: (AQGridView *)inGridView cellForItemAtIndex:
 (NSUInteger) index;
 {
 MagazineCell *cell = (MagazineCell *)[inGridView
 dequeueReusableCellWithIdentifier:@cell];
 if (!cell) {
 cell = [MagazineCell cell];
 *cell.reuseIdentifier = @cell;*
//Assigning to property with 'readonly' atribute not allowed
 }
 cell.backgroundColor = [UIColor clearColor];
 cell.selectionStyle = AQGridViewCellSelectionStyleGlow;
  cell.edicaoLabel.text = [[edicoesArray objectAtIndex:index] name];
cell.dataLabel.text = [[edicoesArray objectAtIndex:index] name];
 return cell;
 }
 
 I tried to do this on head file
 
   @property(nonatomic, readwrite) NSString * reuseIdentifier;
 
 I also tried
 
 @property(nonatomic, assign) NSString * reuseIdentifier;
 
 But still no work.

In what way was the reply you got yesterday not satisfactory? 

The class seems to be modeled on UITableViewCell, in which assignment to the 
reuse identifier is illegal. 

A glance at the class on GitHub suggests the reason: The identifier is intended 
as a key into an NSMutableDictionary. In general, changing the identifier would 
require changing the placement of the cell in the dictionary. AQGridViewCell 
has no reference back to its container, so the change is impossible. Changing 
reuseIdentifier would break AQGridView.

Redeclaring reuseIdentifier won't change an assignment that can't work into one 
that can. All it would do is silence the compiler and leave your app to crash. 
Changing the attribute to assign is a particularly bad idea when the intended 
behavior is copy. Review the memory-management guide to understand why.

I assume you have control over class MagazineCell, and can initialize it as you 
please. Why can't you call [super initWithFrame: CGRectZero reuseIdentifier: 
@cell] in its initializer?

— F

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Help with relationship objects

2011-06-28 Thread Brad Stone
I need a little guidance.  Did I properly add a relationship object?  I don't 
know what's normal in this scenario.

I referred to here: 
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreData/Articles/cdUsingMOs.html

I have an entity called SRNoteEntity which has a to-many relationship with 
SRTodoEntity in a mutableSet called todos.  When I do the following:

[thisNote addTodosObject:todoItem];

I get the below.  Notice the new SRTodoEntity is properly in the todos 
relationship but there's also a reference to it outside of the SRNoteEntity.  
Intuitively, I would think there should be only one reference to it inside 
SRNoteEntity.This is my confusion.  Why is it reference twice?  How can I 
prevent that from happening if this isn't normal?

po [[self managedObjectContext] registeredObjects]
{(
 Note uid:330961758011065
creationDate:2009-09-05 13:21:54 -0400
modificationDate:2009-09-06 12:41:02 -0400
todos:Relationship objects for {(
SRTodoEntity: 0x200483d80 (entity: SRTodoEntity; id: 0x2004121e0 
x-coredata://614245CF-F03E-4F43-9D7F-98CDCB2899FA/SRTodoEntity/p1 ; data: {
note = 0x2002c4e60 
x-coredata://614245CF-F03E-4F43-9D7F-98CDCB2899FA/SRNoteEntity/p1;
todoViewData = nil;
})
)} on 0x200448360
 noteData:538

 SRTodoEntity: 0x200483d80 (entity: SRTodoEntity; id: 0x2004121e0 
x-coredata://614245CF-F03E-4F43-9D7F-98CDCB2899FA/SRTodoEntity/p1 ; data: {
note = 0x2002c4e60 
x-coredata://614245CF-F03E-4F43-9D7F-98CDCB2899FA/SRNoteEntity/p1;
todoViewData = nil;
})
)}

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: sorting a pair of arrays

2011-06-28 Thread Gregory Weston
Mikkel Islay wrote:

 It sounds to me like one way of looking at your data structures is as a 
 NSDictionary with the first array as keys and the second as objects (values).

Which fails the if there's any possibility that the items in the first array 
aren't unique. Better two have an array of dictionaries where each dictionary 
holds a respective pair from the current two arrays as their own objects
{
myInt - 1
myString - some string
}

Then a typical use of either sort descriptors or a comparison proc.
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Reading Adobe Photoshop header, Obj-C

2011-06-28 Thread Jens Alfke

On Jun 28, 2011, at 6:23 AM, Kevin Muldoon wrote:

 As you can see from the output, the 'version'  'reserved' isn't being read 
 at all. Must I coerce/parse something here? Much thanks…

I’m not familiar with the PSD format, but aren’t ‘version’ and ‘reserved’ 
binary? You’re printing them as C strings.

Assuming ‘version’ is a 16-bit big-endian integer, you could read it like this:

const uint8_t* bytes = [signature bytes];
uint16_t version =  *(const uint16_t*)(bytes + 2);
version = NSSwapBigShortToHost(version);

Another way (that I often use) is to define a C struct that matches the fields 
and layout of the header, then cast the bytes to a pointer to that struct and 
read the fields. It makes your code look a lot cleaner, but you have to be 
careful to (a) make sure the compiler packs the struct fields properly, and (b) 
you byte-swap all integer/float fields appropriately.

—Jens

smime.p7s
Description: S/MIME cryptographic signature
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: sorting a pair of arrays

2011-06-28 Thread Jens Alfke

On Jun 28, 2011, at 5:52 AM, Rick C. wrote:

 I have a pair of arrays (one of integers and one of strings) and I need to 
 sort the one of integers from largest to smallest and then however it gets 
 sorted I need to copy that sorting in my strings array since they are 
 related.  Any advice on what the best way to approach this is would be great 
 since I don't have a lot of experience in sorting like this.  Thanks!

Another way to do this, besides what’s been suggested, is to make a new numeric 
array that maps old indexes to sorted indexes. Start it out as the identity 
(a[i] = i), then sort it using a compare function that compares the ints mapped 
into your original int array. Then use the sorted index mapping to renumber 
your original arrays.

—Jens

smime.p7s
Description: S/MIME cryptographic signature
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Reading Adobe Photoshop header, Obj-C

2011-06-28 Thread Kevin Muldoon

Hey Jens,

I suppose the trouble here is that I'm familiar with Objective-C code  
and not so familiar with its parent, C.


On Jun 28, 2011, at 1:00 PM, Jens Alfke wrote:



On Jun 28, 2011, at 6:23 AM, Kevin Muldoon wrote:

As you can see from the output, the 'version'  'reserved' isn't  
being read at all. Must I coerce/parse something here? Much thanks…


I’m not familiar with the PSD format, but aren’t ‘version’ and  
‘reserved’ binary? You’re printing them as C strings.


Assuming ‘version’ is a 16-bit big-endian integer, you could read it  
like this:


const uint8_t* bytes = [signature bytes];
uint16_t version =  *(const uint16_t*)(bytes + 2);
version = NSSwapBigShortToHost(version);

Another way (that I often use) is to define a C struct that matches  
the fields and layout of the header, then cast the bytes to a  
pointer to that struct and read the fields. It makes your code look  
a lot cleaner, but you have to be careful to (a) make sure the  
compiler packs the struct fields properly, and (b) you byte-swap all  
integer/float fields appropriately.


—Jens



Kevin Muldoon
e: caoimgh...@gmail.com

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Reading Adobe Photoshop header, Obj-C

2011-06-28 Thread Jens Alfke

On Jun 28, 2011, at 10:11 AM, Kevin Muldoon wrote:

 I suppose the trouble here is that I'm familiar with Objective-C code and not 
 so familiar with its parent, C. 

Yeah, unfortunately Objective-C does not absolve you of having to know C. With 
where you’re going, I’d suggest cracking open Kernighan  Ritchie or some other 
good C book.

(It’s a shame Cocoa doesn’t have more powerful stream APIs, or some equivalent 
of Perl/PHP/Ruby’s ‘unpack’ function, that would let you do this kind of stuff 
without needing to do pointer grunge. For some reason streams have always been 
a primary Achilles’ heel of Cocoa. I’ve been coding in it for ten years and I 
still never use NSFileHandle — if I have to stream data to/from files I just 
drop down to ANSI C or POSIX I/O calls.)

—Jens

smime.p7s
Description: S/MIME cryptographic signature
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: dispatch_async Performance Issues

2011-06-28 Thread Andreas Grosam

On Jun 28, 2011, at 3:53 PM, Jonathan Taylor wrote:


Hi Jonathan, 

Thank you very much for taking the time to look at it.

 Hi Andreas,
 
 If I understand your post correctly, you are saying that you see a 
 performance drop of 3x when using an iterator in your inner loop as opposed 
 to using hand-written C code to do the iterating.

Yes, at least that was my former assumption.

In the meantime however, I found one (surprising) cause of the performance 
issue. After making the versions  *more* equivalent the issue become apparent.  
I restructured the second version (using the C++ iterators) and will discuss 
this in more detail. The culprit is in the consumer part as follows:

New restructured code:

...
#if defined (USE_FUTURE)
__block size_t sum = 0;
__block size_t total = 0;
#endif

dispatch_async(queue,
 ^{
 CFDataConstBuffers_iteratorchar eof;
 CFDataConstBuffers_iteratorchar iter(*buffersPtr);
 
 size_t sum_ = 0;
 size_t total_ = 0;
 
 while (iter != eof)
 {
 sum_ += *iter;
 ++iter;
 ++total_;
 }
 
#if defined (USE_FUTURE)
 sum = sum_;
 total = total_;
#endif
 semPtr-signal();
 });
 ...


The difference compared to the former code provided in the previous mail is now

1) The C++ instances, that is the iterators, are defined locally within the 
block.

2) The Future (that is the result of the operation) is conditional compiled 
in or out, in order to test its impact.
 Here, the __block modifier is used for the Future variables sum and 
total.  
 When using pointers within the block accessing the outside variables, the 
performance does not differ, but using __block may be more correct.


Note, that I access the variables sum and total only once at the end of the 
block code. This has a reason, which I will explain below.


As mentioned, the conditional  #if define (USE_FUTURE) tests its impact on 
performance.

If USE_FUTURE is defined, the performance drops dramatically!
The same happens, if there are pointer variables which access variables defined 
outside the block.

The performance drops even more if I would use the __block variables sum and 
total directly when incrementing, e.g.:

 while (iter != eof)
 {
 sum += *iter;
 ++iter;
 ++total;
 }

Even when I access the Future sum and total only once in the block, the 
performance penalty is significant.


 Unfortunately you say you haven't actually posted the code relating to the 
 iterator... but it seems to me that this is very likely to be the source of 
 your problems!

OK, appended the source code. :) 
These are three files, and a lot for this mail.
But I guess, this is not the cause of the issue.

 
 Your title states dispatch_async Performance Issues, but have you actually 
 looked into whether you see the same problem in a single-threaded case that 
 does not use dispatch_async?
I haven't *exactly* done this one, but I have a lot of other test cases (see 
below). 
 
 All I can suggest is that you examine the preprocessor output and even the 
 generated assembly code with the aim of spotting any significant differences 
 in the generated code in each case. It may well be that a function call is 
 being generated as part of the iterator code, or something like that. Shark 
 may help you pin down where the problem is, but you will probably need to 
 have some level of appreciation of assembly code to be able to fully 
 interpret the results for your optimized build. 

I examined the assembly and from the looks I couldn't find any hints. Both 
versions looked quite similar. My guess is, it is some synchronization 
primitive like a spin lock.

After modifying the direct implemented and C++ Iterator versions, both run 
now similar if no future is used. If a future is used, for some reason, the 
runtime drops much more for the C++ Iterator version than the code that mimics 
the behavior.

But, considering my original goal, performance is a bit slow, that is, not 
faster then a very primitive implementation which uses one thread, allocates 
NSData buffers, and adds them to a NSMutableData object which is then processed 
(see Classic bench). This is a bit disappointing. Guess the overhead for 
dispatch is still high. Here are some results of my bench marks (note that in 
single threaded approaches the future has no effect since there is none):


 CFDataBuffers Benchmark 
Using  Future: No
Data size: 131072KB, Buffer size: 8192, N = 16384, C = 2
[SimpleProduceConsume1]:Elapsed time: 299.028ms  
[SimpleProduceConsume2]:Elapsed time: 125.744ms
[Classic]:  Elapsed time: 218.383ms
[ConcurrentProduceConsume1]:Elapsed time: 123.748ms
[ConcurrentProduceConsume2]:Elapsed time: 271.387ms
[ConcurrentProduceConsumeIter]: Elapsed time: 265.175ms

 

Re: dispatch_async Performance Issues

2011-06-28 Thread Andreas Grosam

On Jun 28, 2011, at 4:07 PM, Jonathan Taylor wrote:

 ... and more importantly I do not believe your code makes correct use of 
 dispatch_group_async. Your first call adds a producer block to the global 
 concurrent queue, which may start executing immediately. At some time soon 
 afterwards you add a consumer block to the global concurrent queue, which 
 again may start executing immediately - while the producer block is still in 
 the process of producing. As far as I can see the code appears to have a 
 number of major issues:
 
 1. Theoretically your producer block may not have produced anything by the 
 time your consumer block executes

The consumer thread may block until a buffer is available. This happens in 
member function consume() of the buffer list.

 
 2. What happens if your consumer block has processed all the currently-added 
 blocks before the producer block has added the eof marker?
The consumer block loops until a buffer is available, or until an timeout 
occurs.  This happens in member consume().
An empty buffer signals EOF.


 
 3. You have only one single consumer block, which means you will only have a 
 single consumer thread running under Grand Central Dispatch, which rather 
 defeats the purpose of using it at all! I think you may not have fully 
 understood how GCD is intended to be used...
I know this. But there is no easy way around, due to this: the buffers 
represent a contiguous stream of data. The consumer thread is used to execute a 
parser. The parser can not simply stop and then continue with a new chunk of 
data. The parser's state is represented in the execution stack. So, the parser 
may block when it iterates over the data buffers and thus safes its state that 
way.

I know that this is not the optimal way to use GCD. But at least I get 
semaphores which do not trap into the kernel that often, and thread switching 
shall be faster as well.

The original goals for this approach is:

Reduce memory foot print. The buffers list maximum number of buffers is limited.
Increase overall performance due to simultaneously downloading data and parsing.
Scaleability . No need to safe the stream to an intermediate file on disk 
before parsing.
Avoid copying of data completely.




 
 4. Are you sure that your mysterious and rather complicated-seeming 
 CFDataConstBuffers class
:) 
 is sufficiently threadsafe for your producer to be adding to it at the same 
 time as your consumer block is iterating through it? I am 99% sure that the 
 explicit consumer code you wrote in ConcurrentProduceConsume is not 
 threadsafe.
I hope it is. But there may be subtle caveats which can in certain 
circumstances reduce performance due to frequent context switches. I would 
avoid it if I would know IF and when this can happen ;)
  
The source is available (see link previous mail). Would you mind taking a look 
at it? But be warned: this is C++ code.

Thanks in advance!

Andreas

 
 Jonny.

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Reading Adobe Photoshop header, Obj-C

2011-06-28 Thread Kevin Muldoon

No problem at all. I'll pick up Kernighan  Ritchie. Thanks for reply.

On Jun 28, 2011, at 1:17 PM, Jens Alfke wrote:



On Jun 28, 2011, at 10:11 AM, Kevin Muldoon wrote:

I suppose the trouble here is that I'm familiar with Objective-C  
code and not so familiar with its parent, C.


Yeah, unfortunately Objective-C does not absolve you of having to  
know C. With where you’re going, I’d suggest cracking open Kernighan  
 Ritchie or some other good C book.


(It’s a shame Cocoa doesn’t have more powerful stream APIs, or some  
equivalent of Perl/PHP/Ruby’s ‘unpack’ function, that would let you  
do this kind of stuff without needing to do pointer grunge. For some  
reason streams have always been a primary Achilles’ heel of Cocoa.  
I’ve been coding in it for ten years and I still never use  
NSFileHandle — if I have to stream data to/from files I just drop  
down to ANSI C or POSIX I/O calls.)


—Jens



Kevin Muldoon
e: caoimgh...@gmail.com

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Help with relationship objects

2011-06-28 Thread Quincey Morris
On Jun 28, 2011, at 08:40, Brad Stone wrote:

 I get the below.  Notice the new SRTodoEntity is properly in the todos 
 relationship but there's also a reference to it outside of the SRNoteEntity.  
 Intuitively, I would think there should be only one reference to it inside 
 SRNoteEntity.This is my confusion.  Why is it reference twice?  How can I 
 prevent that from happening if this isn't normal?
 
 po [[self managedObjectContext] registeredObjects]
 {(
 Note uid:330961758011065
   creationDate:2009-09-05 13:21:54 -0400
   modificationDate:2009-09-06 12:41:02 -0400
   todos:Relationship objects for {(
   SRTodoEntity: 0x200483d80 (entity: SRTodoEntity; id: 0x2004121e0 
 x-coredata://614245CF-F03E-4F43-9D7F-98CDCB2899FA/SRTodoEntity/p1 ; data: {
   note = 0x2002c4e60 
 x-coredata://614245CF-F03E-4F43-9D7F-98CDCB2899FA/SRNoteEntity/p1;
   todoViewData = nil;
   })
   )} on 0x200448360
 noteData:538
 
 SRTodoEntity: 0x200483d80 (entity: SRTodoEntity; id: 0x2004121e0 
 x-coredata://614245CF-F03E-4F43-9D7F-98CDCB2899FA/SRTodoEntity/p1 ; data: {
   note = 0x2002c4e60 
 x-coredata://614245CF-F03E-4F43-9D7F-98CDCB2899FA/SRNoteEntity/p1;
   todoViewData = nil;
 })
 )}

You've kinda got hold of the wrong end of this. Core Data is an object graph, 
certainly, and any pointer to a managed object is a reference to the object. 
There are references to these objects all over your code -- every stack 
variable pointer is a reference. In the above example, a reference to the Todo 
object exists inside the set of references that represents Note's relationship 
to its Todo's. That makes it a really important reference, but it's certainly 
never the only one.

By sending a 'registeredObjects' message to the managedObjectContext (which is 
*not* an object in the object graph), you've asked it to construct a collection 
(a set, as it happens) of references to Core Data objects it knows about. 
There's no *relationship* from the MOC to the managed objects, it's just the 
MOC's job to know what managed objects are currently in memory.


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Reading Adobe Photoshop header, Obj-C

2011-06-28 Thread Stephen Hoffman

On 28 Jun 2011 10:00:51 -0700, Jens Alfke j...@mooseyard.com posited:

 Assuming Œversion‚ is a 16-bit big-endian integer, you could read it like 
 this:

The htons() and ntohs() C calls go from host-native integer (short) format to 
network format (big-endian), and from network format back to host-native short 
ints.  There are also long versions of these calls.  These calls are also 
implemented to provide correct operations on hosts systems that are little- and 
big-endian; they're portable across platforms.

If you have to do more of this (at the C layer), then you might consider using 
the xdr external data representation calls, too.

And FWIW, going the other way (from knowing C to learning Objective C) can be 
fun, too.

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: sorting a pair of arrays

2011-06-28 Thread Mikkel Islay
On 28 Jun 2011, at 18:40, Gregory Weston wrote:

 Which fails the if there's any possibility that the items in the first array 
 aren't unique. Better two have an array of dictionaries where each dictionary 
 holds a respective pair from the current two arrays as their own objects]

Granted, but in that case you might also consolidate objects for identical 
keys, to retain the natural index and easily inspect related objects.

Mikkel___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: dispatch_async Performance Issues

2011-06-28 Thread Jonathan Taylor
 In the meantime however, I found one (surprising) cause of the performance 
 issue. After making the versions  *more* equivalent the issue become 
 apparent.  I restructured the second version (using the C++ iterators) and 
 will discuss this in more detail. The culprit is in the consumer part as 
 follows:
 
 New restructured code:
 
 [...]
 
 The difference compared to the former code provided in the previous mail is 
 now
 
 1) The C++ instances, that is the iterators, are defined locally within the 
 block.
 
 2) The Future (that is the result of the operation) is conditional compiled 
 in or out, in order to test its impact.
 Here, the __block modifier is used for the Future variables sum and 
 total.  
 When using pointers within the block accessing the outside variables, the 
 performance does not differ, but using __block may be more correct.


Ah - now then! I will take a very strong guess as to what is happening there 
(I've done it myself, and seen it done by plenty of others! [*]). In the case 
where you do NOT define USE_FUTURE, your consumer thread as written in your 
email does not make any use of the variables sum_ and total_. Hence the 
compiler is entirely justified in optimizing out those variables entirely! It 
will still have to check the iterator against eof, and may have to dereference 
the iterator[**], but it does not need to update the sum_ or total_ 
variables. 

It may well be that there is still a deeper performance issue with your 
original code, and I'm happy to have another look at that when I have a chance. 
I suggest you deal with this issue first, though, as it appears to be 
introducing misleading discrepancies in the execution times you're using for 
comparison.

As I say, it's quite a common issue when you start stripping down code with the 
aim of doing minimal performance comparisons. The easiest solution is either to 
printf the results at the end (which forces the compiler to actually evaluate 
them!), or alternatively do the sort of thing you're doing when USE_FUTURE is 
defined - writing to a shadow variable at the end. If you declare your shadow 
variable as volatile then the compiler is forced to write the result and is 
not permitted to optimize everything out.

Hope that helps, even if it may not deal with your original problem yet. 
Apologies that my first round of guesses were wrong - I'm pretty sure about 
this one though :)

Jonny


[**] Completely unrelated to this thread, but see this rather extreme example 
where the claimed performance had to be reduced by a factor of twelve due to 
this problem! 
http://www.ibm.com/developerworks/forums/thread.jspa?threadID=226415
[*] I ~think~ ... because this involves a memory access, which is strictly 
speaking a side effect in itself.

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Reading Adobe Photoshop header, Obj-C

2011-06-28 Thread Thomas Engelmeier

On 28.06.2011, at 15:23, Kevin Muldoon wrote:

 Was writing a program to create thumbnails using SIPS, GhostScript, 
 ImageMagick. During testing I found I needed to read the header file of Adobe 
 Photoshop native documents directly because Duotone image color mode simply 
 wasn't returning the proper color mode (in a variety of programs).
 
 So I threw together this bit of code which works great for the first tag, 
 but
 
   // References
   
 //http://www.adobe.com/devnet-apps/photoshop/fileformatashtml/PhotoshopFileFormats.htm#50577409_pgfId-1055726
   
 //http://www.adobe.com/content/dam/Adobe/en/devnet/photoshop/psir/ps_image_resources.pdf
   
   NSFileHandle *fileHandler = [NSFileHandle 
 fileHandleForReadingAtPath:@/Users/kevin/Desktop/balloon.psd];
   
   NSData *psdFileHeader = [NSData dataWithData:[fileHandler 
 readDataOfLength:26]]; // read entire PSD header
   
   NSData *signature = [psdFileHeader subdataWithRange: NSMakeRange(0,4)];
   NSData *version = [psdFileHeader subdataWithRange: NSMakeRange(4,2)];
   NSData *reserved = [psdFileHeader subdataWithRange: NSMakeRange(6,6)];

You're doing the worst case for performancewise. 

The usual way would be to define a struct 

#pragma pack (push, 2) 
typedef struct {
uint32_t psdMagic;
uint16_t version;
uint16_t reserved1;
uint32_t reserved2;
} PSDHeader;

and use it with code like:

NSData * psdHdrData = [NSData dataWithData:[fileHandler 
readDataOfLength:sizeof( PSDHeader )]]; // read entire PSD header
PSDHeader *header = (psdHeader *) [psdHdrData bytes];
psdMagic = CFEndianBToN( header-psdMagic );


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Reading Adobe Photoshop header, Obj-C

2011-06-28 Thread Thomas Engelmeier

On 28.06.2011, at 21:37, Charles Srstka wrote:

 On Jun 28, 2011, at 12:00 PM, Jens Alfke wrote:
 
 Another way (that I often use) is to define a C struct that matches the 
 fields and layout of the header, then cast the bytes to a pointer to that 
 struct and read the fields. It makes your code look a lot cleaner, but you 
 have to be careful to (a) make sure the compiler packs the struct fields 
 properly, and (b) you byte-swap all integer/float fields appropriately.
 
 It’s my understanding that reading/writing a struct directly to/from a file 
 is considered poor practice, because of the two things you mentioned: endian 
 issues, and the fact that you don’t know how the compiler will align the 
 fields — and even if you test that, you don’t know that it’s not going to 
 change in some future version of clang or gcc, or that Apple’s not going to 
 switch to a new architecture or something in which the compilers will emit 
 different output.

- the endian issue you'll have to deal with anyway
- regarding struct alignment: 
the 80ies file format had tons of of serialized struct data which usually 
better end up in a struct. 
Well, streamed reading has bad performance, so you'd not want to use it in 
image (or movie) manipulation code. 
Streamed reading just to put it back into a struct is another anti-pattern.

The don't is to use binary structs for new defined protocols for transfer, 
for the given reasons.

Regards,
Tom_E


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Menu Title Icon in Cocoa

2011-06-28 Thread Bernard Desgraupes

Hi,

I'd like to know if there is a way of inserting an icon instead of a  
menu title in the menu bar. I'm not speaking of the system status bar,  
but simply of my application's menus. In Carbon, I used to use  
SetMenuTitleIcon(): so the question is is there an equivalent of  
SetMenuTitleIcon in Cocoa ?


Thanks for any clue on this matter, I can't find anything on the web,
Bernard

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Reading Adobe Photoshop header, Obj-C

2011-06-28 Thread Thomas Engelmeier

On 28.06.2011, at 21:57, Thomas Engelmeier wrote:

 
 On 28.06.2011, at 21:37, Charles Srstka wrote:
 
 On Jun 28, 2011, at 12:00 PM, Jens Alfke wrote:
 
 Another way (that I often use) is to define a C struct that matches the 
 fields and layout of the header, then cast the bytes to a pointer to that 
 struct and read the fields. It makes your code look a lot cleaner, but you 
 have to be careful to (a) make sure the compiler packs the struct fields 
 properly, and (b) you byte-swap all integer/float fields appropriately.
 
 It’s my understanding that reading/writing a struct directly to/from a file 
 is considered poor practice, because of the two things you mentioned: endian 
 issues, and the fact that you don’t know how the compiler will align the 
 fields — and even if you test that, you don’t know that it’s not going to 
 change in some future version of clang or gcc, or that Apple’s not going to 
 switch to a new architecture or something in which the compilers will emit 
 different output.
 
 - the endian issue you'll have to deal with anyway
 - regarding struct alignment: 
 the 80ies file format had tons of of serialized struct data which usually 
 better end up in a struct. 
 Well, streamed reading has bad performance, so you'd not want to use it in 
 image (or movie) manipulation code. 

P.S.: unless the structs have variable length data in the middle, i.e. can not 
be read in one read anyway.
IIRC the PS structures are not organized that way, though. 

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Custom NSCell and Bindings

2011-06-28 Thread Sean McBride
On Fri, 24 Jun 2011 09:33:38 -0400, Carter R. Harrison said:

It's a pretty typical situation.  I've got my model objects stored in an
NSSet.  An NSArrayController that is bound to the NSSet.  And then an
NSTableView with one NSTableColumn bound to the arrangedObjects.name
property of the NSArrayController.  It works great.  Now I've introduced
a custom NSCell object into the NSTableView.  The custom cell displays a
title, a subtitle, and a status image - all of which I want to be able
to bind to 3 separate properties of the model object.  How do I do
this?  Right now with the 'value' binding bound to the
arrangedObjects.name property, on the title of my custom cell updates
when the model updates.  If the subtitle or status update in the model
object the cell does not update.  Any help is greatly appreciated.

Basically, you can't. :(

The table column has only one value binding, so can only be bound to one thing, 
not many.  You could change your model to have one property that is an 
amalgamation of your title, subtitle, and image and bind to that.

--

Sean McBride, B. Eng s...@rogue-research.com
Rogue Researchwww.rogue-research.com
Mac Software Developer  Montréal, Québec, Canada


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Custom NSCell and Bindings

2011-06-28 Thread Gustavo Pizano
Hello Carter.

Right now I can't remember the name of the example. 
Its a little tricky but its do-able.

your MO subclass must do something like:

-(NSDictionary *)userDictionary{
return [self dictionaryWithValuesForKeys:[NSArray 
arrayWithObjects:@firstName,@lastName,@avatar,@title,nil]];
}

+(NSSet *)keyPathsForValuesAffectingUserDictionary{
return [NSSet 
setWithObjects:@firstName,@lastName,@avatar,@title,nil];
}

which the right properties.

then in the xib bind the value of the tablecolum to 
arrangedObjects.userDictionary.



then subclass NSCell,  on the awakeFromNib of the controller that contains the 
tableview  tell the TableColum to set its dataCell as the instance of my custom 
cell, (of course I have to initialize my cell before that particular line). 
then in my cell what I do is:

//
//Draw the cell that contains the name of the user andits picutre
//This methods  tales the objectValue which is a NSDictionary, all this is done 
bia bindings,
//the use entoty has a property NSDictionary  keysDictionary which will return 
all the keys of its propertioes with its corresponding values see @ User.m
//
-(void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView{

NSDictionary * cellValues = [self objectValue]; 
NSString * firstName = [cellValues valueForKey:@firstName];
NSString * lastName = [cellValues valueForKey:@lastName];
NSString * completeName = [NSString stringWithFormat:@%@ 
%@,firstName,lastName];  
NSPoint textPoint;
NSDictionary *textAttributes;
NSColor * fontColor;
fontColor = [self isHighlighted]?[NSColor whiteColor]:[NSColor 
blackColor];
textAttributes = [NSDictionary dictionaryWithObjectsAndKeys: fontColor, 
NSForegroundColorAttributeName, [NSFont fontWithName:@Optima 
size:(CGFloat)13.0], NSFontAttributeName, nil];  
NSSize  nameSize = [completeName sizeWithAttributes:textAttributes];
textPoint.x = cellFrame.origin.x + 5.0f;
textPoint.y = cellFrame.origin.y +(cellFrame.size.height - 
nameSize.height)/2.0f - 5.0f ;   
NSRect rectOfDraw = NSMakeRect(textPoint.x, 
textPoint.y,cellFrame.size.width - 50.0f,nameSize.height);
[completeName drawWithRect:rectOfDraw 
options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin
 attributes:textAttributes];

//Getting the Avatr Value 
NSData * avatar = [cellValues valueForKey:@avatar];
NSImage * avatarImage =  [[[NSImage alloc] initWithData:avatar] 
autorelease];//[NSImage imageNamed:@NoAvatar];
[[NSGraphicsContext currentContext] saveGraphicsState]; 
CGFloat yOffset = cellFrame.origin.y;
//Checking if the controll view its flipped or no
//Most probably it is.
//then create an affine tarsnfor and translate its coordinates
if ([controlView isFlipped]) {
NSAffineTransform* xform = [NSAffineTransform transform];
[xform translateXBy:0.0 yBy: cellFrame.size.height];
[xform scaleXBy:1.0 yBy:-1.0];
[xform concat]; 
yOffset = 0-cellFrame.origin.y;
}
//Create and interpolation
//medium  its ok, due we don't need to show a high resolutiuon thumbnail
NSImageInterpolation interpolation = [[NSGraphicsContext 
currentContext] imageInterpolation];
[[NSGraphicsContext currentContext] setImageInterpolation: 
NSImageInterpolationMedium]; 
//configure the image drawrect and size and pritn in on screen
if(avatarImage != nil){
NSRect imageRect ;
imageRect = NSZeroRect;
imageRect.size = [avatarImage size];
NSRect drawingRect;
drawingRect.origin.x = cellFrame.size.width - 40.0f;
drawingRect.origin.y = yOffset + 2.0;
drawingRect.size = NSMakeSize(37.0f, 37.0f);
[avatarImage drawInRect:drawingRect
   fromRect:imageRect 
  operation:NSCompositeSourceOver 
   fraction:0.9];
}  
[[NSGraphicsContext currentContext] setImageInterpolation: 
interpolation];  
[[NSGraphicsContext currentContext] restoreGraphicsState];  


}

basically is jut taking the values from the cell objectvalue which is a 
dictionary and then is just set up of the things inside. So it displays 
correctly the name, the last name and the image of the user, in my case.

Re: Reading Adobe Photoshop header, Obj-C

2011-06-28 Thread Charles Srstka
On Jun 28, 2011, at 2:57 PM, Thomas Engelmeier wrote:

 - the endian issue you'll have to deal with anyway

Yes, that was the point. You already have to go through the struct member by 
member and fix the endians anyway, so reading the whole thing in as one block 
of data won’t actually make your code look that much “cleaner.

 - regarding struct alignment: 
 the 80ies file format had tons of of serialized struct data which usually 
 better end up in a struct. 
 Well, streamed reading has bad performance, so you'd not want to use it in 
 image (or movie) manipulation code. 
 Streamed reading just to put it back into a struct is another anti-pattern.
 
 The don't is to use binary structs for new defined protocols for transfer, 
 for the given reasons.

I am not sure that whatever performance improvement is to be gained by doing 
this is worth the inherent danger in doing this. Certainly I don’t think it’s a 
good idea to recommend this to people who are not familiar with the issues 
involved. Reading/writing structs directly into and out of files seems like 
something that should never be done unless you are very familiar with the way 
the compiler works. By all means, you can read the whole header into a buffer 
at once to cut the fread calls down to one, but I’d still move the values into 
the struct member by member (and of course, you’re going to be doing this 
anyway, as even if you read the data directly into the struct, you’re still 
going to have to go through it and assign bit-swapped values to the struct — 
member by member).

FWIW, Apple also recommends against loading structs directly into and out of 
raw data (here referring to NSData objects, but it’s the same principle) for 
the same reason — you’re essentially relying on undocumented behavior on the 
part of the compiler.

http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Archiving/Articles/codingctypes.html#//apple_ref/doc/uid/20001294-96941

And of course, we don’t even know if performance is a concern to the OP or not. 
This could very likely all be a case of premature optimization, especially if 
creating a whole NSData object for each member is acceptable to him, as in the 
code originally posted.

Charles___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Reading Adobe Photoshop header, Obj-C

2011-06-28 Thread Francis Devereux
On 28 Jun 2011, at 18:00, Jens Alfke wrote:

 Another way (that I often use) is to define a C struct that matches the 
 fields and layout of the header, then cast the bytes to a pointer to that 
 struct and read the fields. It makes your code look a lot cleaner, but you 
 have to be careful to (a) make sure the compiler packs the struct fields 
 properly

You can do this with __attribute__((packed)) - see 
http://developer.apple.com/library/mac/#documentation/DeveloperTools/gcc-4.2.1/gcc/Type-Attributes.html.
 Be aware that __attribute__((packed)) is a GCC extension though, so it may not 
be supported by other compilers.

Francis

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Menu Title Icon in Cocoa

2011-06-28 Thread Marcus Karlsson

On Jun 28, 2011, at 10:00 PM, Bernard Desgraupes wrote:

 Hi,
 
 I'd like to know if there is a way of inserting an icon instead of a menu 
 title in the menu bar. I'm not speaking of the system status bar, but simply 
 of my application's menus. In Carbon, I used to use SetMenuTitleIcon(): so 
 the question is is there an equivalent of SetMenuTitleIcon in Cocoa ?
 
 Thanks for any clue on this matter, I can't find anything on the web,
 Bernard

Sure. The title of each NSMenu is in fact a NSMenuItem and it has a -setImage: 
method that you can call. You can also do this in Interface Builder by 
selecting the menu, selects its underlying NSMenuItem and then choose which 
image you want directly in the inspector.

Marcus



smime.p7s
Description: S/MIME cryptographic signature
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Reading Adobe Photoshop header, Obj-C

2011-06-28 Thread James Merkel

Kevin,

As others have suggested you probably want to step down to more basic  
C coding to handle this task.
You can just use C library functions to read data from the file:  
fopen(), fseek(),  fread(), fgetc(), fclose() etc.  as discussed in  
Kernighan and Ritchie.


As far as big endian, little endian issues, most image files use the  
convention 'MM' for big endian or 'II' for little endian somewhere in  
the first few bytes of the file. You then need to load the data  
accordingly. For example looking at a Photoshop Elements file, I see  
MM' (two ASCII characters) as the first two bytes of the file,  
announcing that the file is big endian.


A tool that may be helpful for you in looking at file data directly is  
a Hex Editor. I use HexEdit which is freeware. Beware-- a Hex Editor  
is a sharp tool, you can open any file on your system and modify it.


Jim Merkel
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Reading Adobe Photoshop header, Obj-C

2011-06-28 Thread John Joyce

On Jun 28, 2011, at 6:18 PM, James Merkel wrote:

 Kevin,
 
 As others have suggested you probably want to step down to more basic C 
 coding to handle this task.
 You can just use C library functions to read data from the file: fopen(), 
 fseek(),  fread(), fgetc(), fclose() etc.  as discussed in Kernighan and 
 Ritchie.
 
 As far as big endian, little endian issues, most image files use the 
 convention 'MM' for big endian or 'II' for little endian somewhere in the 
 first few bytes of the file. You then need to load the data accordingly. For 
 example looking at a Photoshop Elements file, I see MM' (two ASCII 
 characters) as the first two bytes of the file, announcing that the file is 
 big endian.
 
 A tool that may be helpful for you in looking at file data directly is a Hex 
 Editor. I use HexEdit which is freeware. Beware-- a Hex Editor is a sharp 
 tool, you can open any file on your system and modify it.
 
 Jim Merkel

Synalyzeit is a free hex editor that's fabulous.
 ___
 
 Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
 
 Please do not post admin requests or moderator comments to the list.
 Contact the moderators at cocoa-dev-admins(at)lists.apple.com
 
 Help/Unsubscribe/Update your Subscription:
 http://lists.apple.com/mailman/options/cocoa-dev/jjoyce%40apple.com
 
 This email sent to jjo...@apple.com

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Reading Adobe Photoshop header, Obj-C

2011-06-28 Thread James Merkel


On Jun 28, 2011, at 4:35 PM, John Joyce wrote:



On Jun 28, 2011, at 6:18 PM, James Merkel wrote:


Kevin,

As others have suggested you probably want to step down to more  
basic C coding to handle this task.
You can just use C library functions to read data from the file:  
fopen(), fseek(),  fread(), fgetc(), fclose() etc.  as discussed in  
Kernighan and Ritchie.


As far as big endian, little endian issues, most image files use  
the convention 'MM' for big endian or 'II' for little endian  
somewhere in the first few bytes of the file. You then need to load  
the data accordingly. For example looking at a Photoshop Elements  
file, I see MM' (two ASCII characters) as the first two bytes of  
the file, announcing that the file is big endian.


A tool that may be helpful for you in looking at file data directly  
is a Hex Editor. I use HexEdit which is freeware. Beware-- a Hex  
Editor is a sharp tool, you can open any file on your system and  
modify it.


Jim Merkel


Synalyzeit is a free hex editor that's fabulous.


Thanks, I'll take a look at that. HexEdit is getting a little dated.

Jim Merkel
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Reading Adobe Photoshop header, Obj-C

2011-06-28 Thread Jens Alfke

On Jun 28, 2011, at 4:53 PM, James Merkel wrote:

 Synalyzeit is a free hex editor that's fabulous.
 
 Thanks, I'll take a look at that. HexEdit is getting a little dated.

Hex Fiend (open source) is nice too http://ridiculousfish.com/hexfiend/

—Jens

smime.p7s
Description: S/MIME cryptographic signature
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Reading Adobe Photoshop header, Obj-C

2011-06-28 Thread John Joyce

On Jun 28, 2011, at 7:14 PM, Jens Alfke wrote:

 
 On Jun 28, 2011, at 4:53 PM, James Merkel wrote:
 
 Synalyzeit is a free hex editor that's fabulous.
 
 Thanks, I'll take a look at that. HexEdit is getting a little dated.
 
 Hex Fiend (open source) is nice too http://ridiculousfish.com/hexfiend/
 
 —Jens
hex fiend is pretty cool too. 
embeddable no less!___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: sorting a pair of arrays

2011-06-28 Thread Rick C.
Thanks for all of the insight!  I'll be trying out these suggestions and if I 
have any further questions I'll post back...

rc



On Jun 29, 2011, at 2:06 AM, Mikkel Islay wrote:

 On 28 Jun 2011, at 18:40, Gregory Weston wrote:
 
 Which fails the if there's any possibility that the items in the first array 
 aren't unique. Better two have an array of dictionaries where each 
 dictionary holds a respective pair from the current two arrays as their own 
 objects]
 
 Granted, but in that case you might also consolidate objects for identical 
 keys, to retain the natural index and easily inspect related objects.
 
 Mikkel___
 
 Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
 
 Please do not post admin requests or moderator comments to the list.
 Contact the moderators at cocoa-dev-admins(at)lists.apple.com
 
 Help/Unsubscribe/Update your Subscription:
 http://lists.apple.com/mailman/options/cocoa-dev/rickcorteza%40gmail.com
 
 This email sent to rickcort...@gmail.com

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Problem with setNeedsLayout and layoutSubviews in UIScrollView

2011-06-28 Thread Tales Pinheiro de Andrade

Em 27/06/2011, às 13:56, David Duncan escreveu:
 
 The problem is that a UIDeviceOrientaiton also includes Unknown, FaceUp and 
 FaceDown orientations. Thus !Landscape means Portrait or one of those (ditto 
 for !Portrait). A large portion of the time, your likely holding the device 
 in FaceUp orientation as far as UIDeviceOrientation is concerned, and thus 
 failing your Landscape check.
All right, I didn't know about FaceUp and FaceDow. Now makes sense

 
 So, if scroll view layout during scroll, I cannot do this resizing in 
 layoutSubview, and should do this in shouldAutorotateToInterfaceOrientation 
 method in my controller?
 
 No, you would use -willAnimateAutorotationToInterfaceOrientation:duration: to 
 do your layout. But at a higher level, why layout based on orientation in the 
 first place? Unless you have specific elements that should not appear in one 
 orientation or another (which doesn't seem to be the case from your code) why 
 not just layout based on the size of the containing view? You can locally 
 determine if it is a portrait or landscape view by comparing width  height 
 in many cases.
I'm doing this because autoresizingMask wasn't working correctly. I have in 
this scroll view (planed to be visualized mainly in portrait, but also in 
landscape) a view that width  height. If I put just 
UIVireAutoResiningFlexibleHeight | UIVireAutoResiningFlexibleWidth, the view 
get the right size in horizontal , but is compressed in vertical (as when I put 
in landscape, the proportion of dimensions change). And if I used just 
UIVireAutoResiningFlexibleWidth, the subcomponents get not proportionally re 
dimensioned.

I have implemented in shouldAutorotateToInterfaceOrientation and it worked, but 
I will change to willAnimateToInterfaceOrientation:duration: as you proposed. 

Thank you
 
 --
 David Duncan
 

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


how to get viewable text position after scroll a UITextView

2011-06-28 Thread hufey
I have a UITextView and a slide control. The text view has a long
content to read. I need textview can interact with slide control. Now
I can drag slide to move text view text to corresponding percentage by
calculating position of slide control. I want to move slide indicator
position to corresponding position by scroll text view too.
But how I can get current viewable text position? Then I can calculate
its percentage to move slide.
BTW, I can use an inherit UITextView to catch event now.

Thanks,
hufey
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com