顯示具有 iOS 標籤的文章。 顯示所有文章
顯示具有 iOS 標籤的文章。 顯示所有文章

2014年3月11日 星期二

IOS Non Rectangle Button Implement 實現不規格按鈕

Non Rectangle Button Implement

UIButton 在使用有透明的 png 圖檔時

即使點到透明的部份依然會觸發 touch 事件

為了達到更準確的點擊效果

需要繼承 UIButton 類別

修改 -(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event 的觸發條件

並擴展 UIImage 類別的功能

以便根據 UIButton 所造成的圖片縮放取得點擊位置 pixel 資料

建立 UIImage+GetPixelRGBA



























UIImage+GetPixelRGBA.h

@interface UIImage (GetPixelRGBA)

    -(UIColor *)colorAtPoint:(CGPoint)point WithImageSize:(CGSize)size;
    -(UIImage *)reSizeImage:(UIImage *)image toSize:(CGSize)reSize;

@end


UIImage+GetPixelRGBA.m

@implementation UIImage (GetPixelRGBA)

//取得 pixel UIColor
-(UIColor*)colorAtPoint:(CGPoint)point WithImageSize:(CGSize)size{
    
    UIImage *resizeimage = [self reSizeImage:self toSize:size];
    
    CGRect rect = CGRectMake(0.0f, 0.0f, resizeimage.size.width, resizeimage.size.height);
    if (CGRectContainsPoint(rect, point) == NO)  {return nil;}
    
    CGImageRef image = resizeimage.CGImage;
    size_t width = CGImageGetWidth(image);
    size_t height = CGImageGetHeight(image);
    int bytesPerPixel = 4;
    int bytesPerRow = (bytesPerPixel*1);        // 8bpp
    unsigned char pixelData[4] = {0, 0, 0, 0};
    
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(pixelData, 1, 1, 8, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast|kCGBitmapByteOrder32Big);
    CGColorSpaceRelease(colorSpace);
    
    if (context == NULL)  {
        NSLog(@"[colorAtPixel] Unable to create context!");
        return nil;
    }
    
    CGContextSetBlendMode(context, kCGBlendModeCopy);
    
    CGFloat pointX = point.x;
    CGFloat pointY = height-point.y;
    CGContextTranslateCTM(context, -pointX, -pointY);
    CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, (CGFloat)width, (CGFloat)height), image);
    CGContextRelease(context);
    
    //Convert color values [0..255] to floats [0.0..1.0]
    CGFloat red = (CGFloat)pixelData[0]/255.0f;
    CGFloat green = (CGFloat)pixelData[1]/255.0f;
    CGFloat blue = (CGFloat)pixelData[2]/255.0f;
    CGFloat alpha = (CGFloat)pixelData[3]/255.0f;
    return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];

}

//改變 UIIamge 圖片大小
-(UIImage *)reSizeImage:(UIImage *)image toSize:(CGSize)reSize
{
    UIGraphicsBeginImageContext(CGSizeMake(reSize.width, reSize.height));
    [image drawInRect:CGRectMake(0, 0, reSize.width, reSize.height)];
    UIImage *reSizeImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return reSizeImage;
}

@end


建立 NonRectButton 
















直接覆寫 UIButton 原有方法

另外記得要 import 剛剛擴展的 UIImage 方法

NonRectButton.m

#import "NonRectButton.h"
#import "UIImage+GetPixelRGBA.h"

@implementation NonRectButton

-(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event  {
    
    //  We can't test the image's alpha channel if the button has no image. Fall back to super.
    UIImage *image = [self backgroundImageForState:UIControlStateNormal];
    if (image == nil)  {return YES;}
    //NSLog(@"b %@", NSStringFromCGPoint(point));
    CGColorRef color = [[image colorAtPoint:point WithImageSize:self.frame.size] CGColor];
    CGFloat alphaValue = CGColorGetAlpha(color);
    return (alphaValue >= 0.1f);
}

@end

最後在 ViewController 上實作 NonRectButton

記得要 import NonRectButton.h

MainViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view.
    [self.view setBackgroundColor:[UIColor whiteColor]];
    NonRectButton *button = [NonRectButton buttonWithType:UIButtonTypeCustom];
    [button setFrame:CGRectMake(60, 60, 200, 200)];
    [button setBackgroundImage:[UIImage imageNamed:@"star.png"] forState:UIControlStateNormal];
    [self.view addSubview:button];
}

出來的結果
























若成功的話

將只會在 touch 到有顏色的部分時觸發點擊事件
























End ~








2013年9月30日 星期一

Xcode5 IOS7 SDK 不使用 ARC 與 Storyboards

Xcode 5 without ARC and Storyboards

在 Xcode5 的 ios 開發中

Apple 拿掉了 Use Stroyboards 與 Use Automatic Reference Counting 的選項

對於不習慣使用 Storyboard 與 Arc 的人造成一定的困擾(我自己...)

這邊介紹怎麼在 Xcode5 下不使用 Storyboards 與 Arc 來開發

一樣開啟 Xcode5 建立一個專案

選擇 Empty Application

















建立完成後大該長這個樣
















接著先至 Project 的 Build Settings 將 ARC 的使用改為 no
















再來就至專案下建立熟悉的 ViewController

習慣使用 xib 開發的記得要將 xib 的選項打勾

因為本人不習慣使用 xib 所以就沒勾了
















Creat 後會長這樣
















開始寫 code

AppDelegate.h

#import "MainViewController.h"  //import viewcontroller

@interface AppDelegate : UIResponder<UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (retain, nonatomic) MainViewController *viewcontroller_main;  //define viewcontroller

@end

AppDelegate.m 的部分要注意將 ARC 設為 no 之後就可以加上 autorelease 了

AppDelegate.m

-(void)dealloc{
    
    [_viewcontroller_main release];
    
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    
    self.viewcontroller_main = [[MainViewController alloc]initWithNibName:nil bundle:nil];
    
    [self.window setRootViewController:self.viewcontroller_main];
    
    [self.viewcontroller_main release];
    
    [self.window makeKeyAndVisible];
    
    return YES;

}

MainViewController.m 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [self.view setBackgroundColor:[UIColor whiteColor]];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view.
    UIAlertView *alertview = [[UIAlertView alloc]initWithTitle:@"hello world" message:nil delegate:nil cancelButtonTitle:@"ok" otherButtonTitles: nil];
    [alertview show];
    [alertview release];
}

最後模擬器下的結果應該會像這樣




2013年9月17日 星期二

IOS QRcode Generator 二維條碼產生器

提供一個簡單的二維條碼產生器

Development environment

Xcode 4.6.3

iOS-QR-Code-Encoder   Update: Nov 28,2012

https://github.com/moqod/iOS-QR-Code-Encoder 


Start:

建立一個 single view project














將下載下來的檔案案解壓縮後

將需要使用到的檔案拉到專案內自己習慣放 Library 的位置

需要用到的有 libqrencode 資料夾內的所有檔案

跟 Classes 內的 QRCodeGenerator.h 和 QRCodeGenerator.m

拉完後大該會像這樣














拉完後記得將 QRCodeGenerator.m 的 Target Membership 打勾













不然在 Compiler 時會出現錯誤

接著就開始 coding

ViewController.m

    
#import "QRCodeGenerator.h"
    
- (void)viewDidLoad
{
    [super viewDidLoad];
   
    //要轉換為 qrcode 的字串
    NSString *str_qrcode = @"https://www.google.com.tw/";

    //定義與初始畫 uiimageview 用來放產生的 qrcode
    UIImageView *imgview_qrcode = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 320)];
    
    imgview_qrcode.backgroundColor = [UIColor whiteColor];

    //讓 uiimageview 的 image 為 qrcodegenerator 產生的 qrcode image 
    imgview_qrcode.image = [QRCodeGenerator qrImageForString:str_qrcode imageSize:imgview_qrcode.bounds.size.width];
   
    //將 uiimageview 加入主畫面中
    [self.view addSubview:imgview_qrcode];
    
    [imgview_qrcode release];
    
}
   

就這樣

你可以產生出一個內容為 google 網址的 QRcode










2013年3月24日 星期日

IOS Zxing QRcode 教學

使用 Xzing Library 寫一個簡單的 QRcode 掃瞄器

先至 Xzing 下載最新版的 Library

https://code.google.com/p/zxing/

寫本篇文章時 

Xcode Version : 4.6.1

Zxing Version : 2.1

先建立一個Single View Application Project



本專案只實作iPhone Device



專案建立完成後將下載下來的 Zxing-2.1 解壓縮後找到裡面的 iphone 跟 cpp 資料夾



複製到剛剛建的專案目錄下



接著將專案目錄下的 iphone/ZXingWidget/ZXingWidget.xcodeproj



拖拉至剛剛建的 Project 專案下面



接著是專案內 Build 的設定

點選 ZXingWidget.xcodeproj 至 TARGETS Build Settings 下的 Other Warning Flags

加入一條 -Wno-unused-private-field 

(解決錯誤訊息 private field 'cached_y_' is not used ...等的問題)



點選專案至 Targets Build Settings 下的 Header Search Paths

加入 iphone/ZXingWidget/Classes     recursive

跟 cpp/core/src    non-recursive



至 Targets Build Settings 下的 C++ Standard Library 改為 Compiler Default

(解決錯誤訊息 apple mach o linker error ...等的問題)

 

至 Targets Build Phases 下加入一個 Target Dependencies

ZxingWidget (ZxingWidget)

至 Targets Build Phases 下加入六個 Link Binary Libraries

libZxingWidget.a
AudioToolbox.framework
AVFoundation.framework
CoreMedia.framework
CoreVideo.framework
libiconv.dylib



最後將 ViewController.m 檔名改為 ViewController.mm

(解決錯誤訊息 iostream file not found ...等的問題)



最後為 ViewController.h 跟 ViewController.mm 內的 Code

ViewController.h
#import <UIKit/UIKit.h>
#import "ZXingWidgetController.h"
@interface ViewController : UIViewController<zxingdelegate>
    -(IBAction)scanButton:(id)sender;
@end

ViewController.mm
#import "ViewController.h"

#ifndef ZXQR
#define ZXQR 1
#endif

#if ZXQR
#import "QRCodeReader.h"
#endif

#ifndef ZXAZ
#define ZXAZ 0
#endif

#if ZXAZ
#import "AztecReader.h"
#endif

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
 // Do any additional setup after loading the view, typically from a nib.
    UIButton *scan_button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [scan_button setFrame:CGRectMake(0, 0, 320, 50)];
    [scan_button setTitle:@"scan" forState:UIControlStateNormal];
    [scan_button addTarget:self action:@selector(scanButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:scan_button];
}

-(IBAction)scanButton:(id)sender{
    ZXingWidgetController *widController = [[ZXingWidgetController alloc] initWithDelegate:self showCancel:YES OneDMode:NO];
    
    NSMutableSet *readers = [[NSMutableSet alloc ] init];
    
#if ZXQR
    QRCodeReader* qrcodeReader = [[QRCodeReader alloc] init];
    [readers addObject:qrcodeReader];
    [qrcodeReader release];
#endif
    
#if ZXAZ
    AztecReader *aztecReader = [[AztecReader alloc] init];
    [readers addObject:aztecReader];
    [aztecReader release];
#endif
    
    
    widController.readers = readers;
    [readers release];
    
    [self presentModalViewController:widController animated:YES];
    [widController release];
}

#pragma mark -
#pragma mark ZXingDelegateMethods

- (void)zxingController:(ZXingWidgetController*)controller didScanResult:(NSString *)result {
    [self dismissModalViewControllerAnimated:NO];
    NSLog(@"%@",result);
}

- (void)zxingControllerDidCancel:(ZXingWidgetController*)controller {
    [self dismissModalViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

在設備上的結果



按下 Button 後會開啟掃描畫面



掃描 QRcode 後會執行 NSLog(@"%@",result);

沒意外的話 All Output 會將結果顯示出來



END.