我试图改变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)
这段代码有效,但获得的图像不是很好:返回图像的边界像素是间歇性的,不像我的第一张图像那么平滑.我该如何解决这个问题?
我正在尝试在我的项目中使用可达性.我添加了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) 我有一个方法,我想在后面-viewDidLoad和后台线程中调用.有没有办法结合这两种方法:
[self performSelector:(SEL) withObject:(id) afterDelay:(NSTimeInterval)]
和
[self performSelectorInBackground:(SEL) withObject:(id)]?
我有两个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 一样的错误.我该如何处理这个问题?谢谢.
我试图向我的 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)
有什么建议么?
我想绘制带有圆角的对角线。我的代码:
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) 我正在进行聊天应用程序并从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) 我正在研究一个包含类别的简单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文件中我合成了我的numerator和denominator.在main.m我Fraction班级的创建类别中:
#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) - (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的所有子视图.我怎么解决这个问题?
我有iOS 6开发的项目,当我在iOS7设备上安装它看起来不错.但现在我将xcode更新为第5版,现在看起来不太好.我知道应用程序迁移.我的问题是,如果我继续使用xcode 4并上传到Apple将针对iOS 6,我会遇到一些问题吗?
我是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) 例如:
NSString *sentence = @"My name is Roman";
NSString *name = @"Roman";
Run Code Online (Sandbox Code Playgroud)
有没有办法name从sentence字符串中删除文本?
我的代码:
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切换到静音模式时,声音仍在播放.我怎么解决这个问题?