小编Rom*_*use的帖子

改变UIImage颜色

我试图改变UIImage的颜色.我的代码:

-(UIImage *)coloredImage:(UIImage *)firstImage withColor:(UIColor *)color {
    UIGraphicsBeginImageContext(firstImage.size);

    CGContextRef context = UIGraphicsGetCurrentContext();
    [color setFill];

    CGContextTranslateCTM(context, 0, firstImage.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);

    CGContextSetBlendMode(context, kCGBlendModeCopy);
    CGRect rect = CGRectMake(0, 0, firstImage.size.width, firstImage.size.height);
    CGContextDrawImage(context, rect, firstImage.CGImage);

    CGContextClipToMask(context, rect, firstImage.CGImage);
    CGContextAddRect(context, rect);
    CGContextDrawPath(context,kCGPathElementMoveToPoint);

    UIImage *coloredImg = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return coloredImg;
}
Run Code Online (Sandbox Code Playgroud)

这段代码有效,但获得的图像不是很好:返回图像的边界像素是间歇性的,不像我的第一张图像那么平滑.我该如何解决这个问题?

iphone objective-c uiimage cgcontext ios

87
推荐指数
6
解决办法
8万
查看次数

Apple的Reachability文件中存在奇怪的错误

我正在尝试在我的项目中使用可达性.我添加了Reachability.h和Reachability.m文件.但在构建项目之后,xCode显示了奇怪的错误:

Undefined symbols for architecture i386:
"_SCNetworkReachabilityCreateWithName", referenced from:
  +[Reachability reachabilityWithHostname:] in Reachability.o
"_SCNetworkReachabilityCreateWithAddress", referenced from:
  +[Reachability reachabilityWithAddress:] in Reachability.o
"_SCNetworkReachabilitySetCallback", referenced from:
  -[Reachability startNotifier] in Reachability.o
  -[Reachability stopNotifier] in Reachability.o
"_SCError", referenced from:
  -[Reachability startNotifier] in Reachability.o
"_SCErrorString", referenced from:
  -[Reachability startNotifier] in Reachability.o
"_SCNetworkReachabilitySetDispatchQueue", referenced from:
  -[Reachability startNotifier] in Reachability.o
  -[Reachability stopNotifier] in Reachability.o
"_SCNetworkReachabilityGetFlags", referenced from:
  -[Reachability isReachable] in Reachability.o
  -[Reachability isReachableViaWWAN] in Reachability.o
  -[Reachability isReachableViaWiFi] in Reachability.o
  -[Reachability connectionRequired] in Reachability.o
  -[Reachability isConnectionOnDemand] in Reachability.o
  -[Reachability …
Run Code Online (Sandbox Code Playgroud)

objective-c ios

17
推荐指数
1
解决办法
7779
查看次数

如何在延迟和后台线程中调用方法

我有一个方法,我想在后面-viewDidLoad和后台线程中调用.有没有办法结合这两种方法:

[self performSelector:(SEL) withObject:(id) afterDelay:(NSTimeInterval)]

[self performSelectorInBackground:(SEL) withObject:(id)]

iphone objective-c ios

14
推荐指数
2
解决办法
1万
查看次数

在Swift 3中相互引用的结构

我有两个CoreData实体,它们具有一对一的关系.我想基于这个实体创建结构. 我的代码:

struct DetailedPin {
     var pin: Pin?
}

struct Pin {
    var detailedPin: DetailedPin?  
}
Run Code Online (Sandbox Code Playgroud)

但是我收到了一个错误:Value type 'DetailedPin' cannot have a stored property that references itself.和Pinstruct 一样的错误.我该如何处理这个问题?谢谢.

struct swift

12
推荐指数
2
解决办法
5259
查看次数

SQLite。无法添加超过 1000 行

我试图向我的 SQLite 数据库(使用 fmdb)添加 10k 行,但写入在 1000 行处停止。我没有任何错误或警告。我的代码:

NSString *queryString = [NSString stringWithFormat:@"insert into histories (storyid, text, date, storyurl) values (%li, ?, ?, ?)",history.storyIndex];
if ([self.db open]) {
    if([self.db executeUpdate:queryString, history.storyText, history.storyDate, history.storyUrl]) {
        NSLog(@"history added");
    } 
}
Run Code Online (Sandbox Code Playgroud)

有什么建议么?

sqlite iphone objective-c fmdb ios

4
推荐指数
1
解决办法
3008
查看次数

CAShapeLayer 线的圆角

我想绘制带有圆角的对角线。我的代码:

class DiagonalView: UIView {

// MARK: - Public Properties

@IBInspectable var fillColor: UIColor = UIColor.white {
    didSet {
        self.diagonalLayer.strokeColor = fillColor.cgColor
        self.redraw()
    }
}

@IBInspectable var lineWidth: CGFloat = 1 {
    didSet {
        self.diagonalLayer.lineWidth = lineWidth
        self.redraw()
    }
}

// MARK: - Private Properties

private var diagonalLayer: CAShapeLayer = {
    let layer = CAShapeLayer()
    layer.fillColor = UIColor.clear.cgColor
    layer.strokeColor = UIColor.white.cgColor
    layer.lineCap = kCALineCapRound
    layer.lineWidth = 1
    return layer
}()

// MARK: - Constructors

override func awakeFromNib() {
    super.awakeFromNib()
    self.layer.addSublayer(self.diagonalLayer) …
Run Code Online (Sandbox Code Playgroud)

cashapelayer ios swift3

4
推荐指数
1
解决办法
1429
查看次数

单击按钮后重新加载TableView数据

我正在进行聊天应用程序并从SQLite数据库中读取消息数据.我想在点击"发送"按钮后用新消息重新加载我的TableView数据.我按钮的事件:

    [_sendButton addTarget:self action:@selector(saveMessage:) forControlEvents:UIControlEventTouchUpInside];
Run Code Online (Sandbox Code Playgroud)

我想我需要使用,-(void)viewDidAppear:(BOOL)animated但它没有用(或者我做错了).

[_tableView reloadData];

-(void)viewDidAppear:(BOOL)animated {

sqlite3 *database;

databaseName = @"Messenges.db";
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
databasePath = [documentsDir stringByAppendingPathComponent:databaseName];

messenges = [[NSMutableArray alloc] init];

if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
    const char *sqlStatement = "select * from messenges";
    sqlite3_stmt *compiledStatement;
    if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
        while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
            NSString *dbMessageText = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];

            Message *messege = [[Message alloc] initWithName:dbMessageText];

            [messenges addObject:messege]; …
Run Code Online (Sandbox Code Playgroud)

sqlite objective-c tableview ios

3
推荐指数
1
解决办法
3780
查看次数

分类.变量的可见性

我正在研究一个包含类别的简单Objective-c程序.我的班.h:

#import <Foundation/Foundation.h>

@interface Fraction : NSObject

@property int numerator, denominator;

-(void)setNumerator:(int)n andDenominator:(int)d;

@end
Run Code Online (Sandbox Code Playgroud)

在.m文件中我合成了我的numeratordenominator.在main.mFraction班级的创建类别中:

#import "Fraction.h"    
@interface Fraction (MathOps)
-(Fraction *) add: (Fraction *) f;
@end

@implementation Fraction (MathOps)
-(Fraction *) add: (Fraction *) f
{
    // To add two fractions:
    // a/b + c/d = ((a*d) + (b*c)) / (b * d)
    Fraction *result = [[Fraction alloc] init];
    result.numerator = (numerator * f.denominator) +
    (denominator * f.numerator);
    result.denominator = denominator …
Run Code Online (Sandbox Code Playgroud)

objective-c categories

2
推荐指数
1
解决办法
446
查看次数

removeFromSuperview删除所有子视图

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    NSLog(@"will rotation");

    for (UIButton *button in self.view.subviews) {
        [button removeFromSuperview];
    }

}
Run Code Online (Sandbox Code Playgroud)

我有这个代码的问题.我需要从我的视图中删除UIButtons.但是这段代码也删除了我的self.view的所有子视图.我怎么解决这个问题?

iphone objective-c ios

2
推荐指数
1
解决办法
1698
查看次数

需要迁移项目到iOS 7吗?

我有iOS 6开发的项目,当我在iOS7设备上安装它看起来不错.但现在我将xcode更新为第5版,现在看起来不太好.我知道应用程序迁移.我的问题是,如果我继续使用xcode 4并上传到Apple将针对iOS 6,我会遇到一些问题吗?

xcode objective-c ios ios6 ios7

2
推荐指数
1
解决办法
1061
查看次数

理解和编写单例类而不是使用委托类

我是iOS编程的新手,我对单例类是什么以及使用它的原因很感兴趣.我找到了一些信息,但它含糊不清.特别是我想将它应用于实例.我的项目使用Facebook SDK,我想为我的NSDictionary创建包含好友列表的单例类.我的.m委托文件:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//here is some other code

facebook = [[Facebook alloc] initWithAppId:@"my app id" andDelegate:self];

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:@"FBAccessTokenKey"] 
    && [defaults objectForKey:@"FBExpirationDateKey"]) {
    facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
    facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];
}

if (![facebook isSessionValid]) {
    NSArray *permissions = [[NSArray alloc] initWithObjects:
                            @"user_location", 
                            @"friends_location",
                            @"read_friendlists",
                            nil];
    [facebook authorize:permissions];
    [permissions release];
}
[facebook requestWithGraphPath:@"me/friends" andDelegate:(id)self];

//here is some other code
}
Run Code Online (Sandbox Code Playgroud)

我设置了我的NSDictionary值请求返回朋友列表:

- (void)request:(FBRequest *)request didLoad:(id)result {
_friendsDictionary = result;
} …
Run Code Online (Sandbox Code Playgroud)

singleton facebook objective-c ios

1
推荐指数
1
解决办法
2286
查看次数

如何从另一个NSString中删除NSString的文本

例如:

NSString *sentence = @"My name is Roman";
NSString *name  = @"Roman";
Run Code Online (Sandbox Code Playgroud)

有没有办法namesentence字符串中删除文本?

objective-c nsstring ios

0
推荐指数
2
解决办法
136
查看次数

如果打开静音模式,如何停止播放声音

我的代码:

NSString *soundName = [[NSBundle mainBundle] pathForResource:@"music" ofType:@"mp3"];
NSURL *soundURL = [NSURL fileURLWithPath:soundName];
NSError *error = [[NSError alloc] init];
self.backgroundPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:&error];

if (self.backgroundPlayer == nil) {
    NSLog(@"error = %@",[error description]);
} else {
    [self.backgroundPlayer setDelegate:self];
    [self.backgroundPlayer setNumberOfLoops:HUGE_VAL];
    [self.backgroundPlayer setVolume:0.5f];
    [self.backgroundPlayer prepareToPlay];
    if ([self.backgroundPlayer prepareToPlay]) {
        if ([self.backgroundPlayer play]) {
            NSLog(@"playing");
        }
    } else {
        NSLog(@"error!");
    }
}
Run Code Online (Sandbox Code Playgroud)

当我将iPhone切换到静音模式时,声音仍在播放.我怎么解决这个问题?

iphone objective-c avaudioplayer ios

0
推荐指数
1
解决办法
1428
查看次数