RSS

(root)/iphone/common : 67 : source/ImageWell.m

To get this branch, use:
bzr branch /browse/iphone/common

« back to all changes in this revision

Viewing changes to source/ImageWell.m

Dömötör Gulyás
2010-07-08 00:20:46
Revision ID: dognotdog@gmail.com-20100707222046-da30ix8xg7fah23n
fixes colorspace bug in imagescaling resulting in failure for non-RGB images; changes image well behavior to show alert for choosing the image source, as well as show a progress indicator

Show diffs side-by-side

added added

removed removed

73
73
        // per component. Regardless of what the source image format is
74
74
        // (CMYK, Grayscale, and so on) it will be converted over to the format
75
75
        // specified here by CGBitmapContextCreate.
76
 
        CGColorSpaceRef colorspace = CGImageGetColorSpace(image);
 
76
        //CGColorSpaceRef colorspace = CGImageGetColorSpace(image);
 
77
        CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
77
78
        CGContextRef context = CGBitmapContextCreate(bitmapData,dstWidth,dstHeight,8,bitmapBytesPerRow,
78
79
        colorspace,kCGImageAlphaPremultipliedFirst);
79
80
        CGColorSpaceRelease(colorspace);
291
292
@end
292
293
 
293
294
 
 
295
@interface ImageWell (Private)
 
296
- (void) showImagePicker;
 
297
- (void) pasteAction: (id) info;
 
298
 
 
299
@end
294
300
 
295
301
@implementation ImageWell
296
302
 
308
314
    if (!(self = [super initWithCoder: coder]))
309
315
                return nil;
310
316
        
311
 
        instructionsView = [[UITextView alloc] initWithFrame: CGRectInset([self bounds], 0.0f, 0.0f)];
 
317
        instructionsView = [[[UITextView alloc] initWithFrame: CGRectInset([self bounds], 0.0f, 0.0f)] autorelease];
312
318
        [instructionsView setText: NSLocalizedString(@"ImageWell instruction text", @"\n\n\nTap and hold to paste image,\n\nor\n\nDouble-tap to select image from gallery.")];
313
319
        //[instructionsView setBackgroundColor: [UIColor yellowColor]];
314
320
        [instructionsView setTextColor: [UIColor whiteColor]];
315
321
        [instructionsView setBackgroundColor: [UIColor clearColor]];
316
 
        [instructionsView setFont: [UIFont systemFontOfSize: [UIFont labelFontSize]]];
 
322
        [instructionsView setFont: [UIFont systemFontOfSize: [UIFont labelFontSize]*2.0f]];
317
323
        [instructionsView setEditable: NO];
318
324
        [instructionsView setUserInteractionEnabled: NO];
319
325
        [instructionsView setTextAlignment: UITextAlignmentCenter];
320
326
        [self addSubview: instructionsView];
321
327
 
322
328
        [instructionsView layer].opacity = 0.0f;
 
329
        
 
330
        activityIndicatorView = [[[UIActivityIndicatorView alloc] initWithFrame: [self bounds]] autorelease];
 
331
        activityIndicatorView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
 
332
        activityIndicatorView.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin |
 
333
                                                                                        UIViewAutoresizingFlexibleRightMargin |
 
334
                                                                                        UIViewAutoresizingFlexibleTopMargin |
 
335
                                                                                        UIViewAutoresizingFlexibleBottomMargin);
 
336
        [activityIndicatorView sizeToFit];
 
337
 
 
338
        [activityIndicatorView layer].opacity = 0.0f;
 
339
        [self addSubview: activityIndicatorView];
 
340
        
 
341
        CGRect bounds = [self bounds];
 
342
        CGSize a = [activityIndicatorView frame].size;
 
343
        [activityIndicatorView setFrame: CGRectMake((bounds.size.width-a.width)*0.5f, (bounds.size.height-a.height)*0.5f, a.width, a.height)];
323
344
 
324
345
 
325
346
        return self;
342
363
 
343
364
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
344
365
{
 
366
        switch(buttonIndex)
 
367
        {
 
368
                case 0:
 
369
                        break;
 
370
                case 1:
 
371
                        [self showImagePicker];
 
372
                        break;
 
373
                case 2:
 
374
                        [self pasteAction: nil];
 
375
                        break;
 
376
        }
 
377
 
 
378
 
345
379
        [alertView dismissWithClickedButtonIndex: buttonIndex animated: YES];
 
380
        [activityIndicatorView stopAnimating];
 
381
        [activityIndicatorView layer].opacity = 0.0;
346
382
}
347
383
 
348
384
 
353
389
        [alert autorelease];
354
390
}
355
391
 
 
392
- (UIImage*) pbImage
 
393
{
 
394
        UIImage* theImage = nil;
 
395
        NSArray* images = [[UIPasteboard generalPasteboard] images];
 
396
        if ([images count])
 
397
        {
 
398
                UIImage* img = [images objectAtIndex: 0];
 
399
                if (img)
 
400
                        theImage = img;
 
401
        }
 
402
        else
 
403
        {
 
404
                id url = [[UIPasteboard generalPasteboard] URL];
 
405
                if (url)
 
406
                {
 
407
                        NSData *data = [NSData dataWithContentsOfURL:url];
 
408
                        UIImage *img = [UIImage imageWithData:data];
 
409
                        if (img)
 
410
                        {
 
411
                                theImage = img;
 
412
                        }
 
413
                }
 
414
        }
 
415
        return theImage;
 
416
}
 
417
 
356
418
- (void) pasteAction: (id) info
357
419
{
358
420
        NSArray* images = [[UIPasteboard generalPasteboard] images];
432
494
                [anim queueForKey: @"image-opacity" atBeginning: NO cancelPending: NO];
433
495
}
434
496
 
 
497
- (void) showImagePicker
 
498
{
 
499
        if (!imagePicker)
 
500
        {
 
501
                imagePicker = [[UIImagePickerController alloc] init];
 
502
 
 
503
                //[imagePicker setSourceType: UIImagePickerControllerSourceTypeCamera];
 
504
                [imagePicker setDelegate: self];
 
505
                //[imagePicker setAllowsImageEditing: YES];
 
506
                //[imagePicker setShowsCameraControls: NO];
 
507
        }
 
508
        
 
509
        assert(delegate);
 
510
        #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 30200
 
511
        if ([self isItAPad])
 
512
        {
 
513
                if (!popoverController)
 
514
                {
 
515
                        Class popoverClass = NSClassFromString(@"UIPopoverController");
 
516
                        popoverController = [[popoverClass alloc] initWithContentViewController: imagePicker];
 
517
                }
 
518
                [popoverController presentPopoverFromRect: [self bounds] inView: self permittedArrowDirections: UIPopoverArrowDirectionAny animated: YES];
 
519
        }
 
520
        else
 
521
        #endif
 
522
                [delegate presentModalViewController: imagePicker animated: YES];
 
523
}
 
524
 
435
525
- (void) touchesBegan: (NSSet*) touches withEvent: (UIEvent*) event
436
526
{
437
 
        UITouch* touch = [touches anyObject];
 
527
/*      UITouch* touch = [touches anyObject];
438
528
        
439
529
        if (([touch tapCount] == 1) && [self isItAPhone])
440
530
        {
451
541
                [pasteTimer invalidate];
452
542
                pasteTimer = nil;
453
543
 
454
 
                if (!imagePicker)
455
 
                {
456
 
                        imagePicker = [[UIImagePickerController alloc] init];
457
 
 
458
 
                        //[imagePicker setSourceType: UIImagePickerControllerSourceTypeCamera];
459
 
                        [imagePicker setDelegate: self];
460
 
                        //[imagePicker setAllowsImageEditing: YES];
461
 
                        //[imagePicker setShowsCameraControls: NO];
462
 
                }
463
 
                
464
 
                assert(delegate);
465
 
                #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 30200
466
 
                if ([self isItAPad])
467
 
                {
468
 
                        if (!popoverController)
469
 
                        {
470
 
                                Class popoverClass = NSClassFromString(@"UIPopoverController");
471
 
                                popoverController = [[popoverClass alloc] initWithContentViewController: imagePicker];
472
 
                        }
473
 
                        [popoverController presentPopoverFromRect: [self bounds] inView: self permittedArrowDirections: UIPopoverArrowDirectionAny animated: YES];
474
 
                }
475
 
                else
476
 
                #endif
477
 
                        [delegate presentModalViewController: imagePicker animated: YES];
 
544
                [self showImagePicker];
478
545
        }
 
546
 
 
547
*/
 
548
}
 
549
 
 
550
- (void) showSourceDialog
 
551
{
 
552
        NSString* msg = NSLocalizedString(@"ImageWell alert message", @"");
 
553
        NSString* title = NSLocalizedString(@"ImageWell alert title", @"");
 
554
        
 
555
        UIImage* img = [self pbImage];
 
556
 
 
557
        UIAlertView* alert = [[UIAlertView alloc] initWithTitle: title message: msg delegate: self cancelButtonTitle: NSLocalizedString(@"ImageWell alert cancel", @"")
 
558
                otherButtonTitles:      NSLocalizedString(@"ImageWell alert picker", @""),
 
559
                                                        (img ? NSLocalizedString(@"ImageWell alert paste", @"") : nil),
 
560
                                                        nil];
 
561
        [alert show];
 
562
        [alert autorelease];
 
563
        
479
564
}
480
565
 
481
566
- (void) touchesEnded: (NSSet*) touches withEvent: (UIEvent*) event
484
569
        
485
570
        [pasteTimer invalidate];
486
571
        pasteTimer = nil;
 
572
 
 
573
        [activityIndicatorView layer].opacity = 1.0;
 
574
        [activityIndicatorView startAnimating];
 
575
        [NSTimer scheduledTimerWithTimeInterval: 0.01 target: self selector: @selector(showSourceDialog) userInfo: nil repeats: NO];
487
576
        
488
577
}
489
578
 

Loggerhead 1.17 is a web-based interface for Bazaar branches