在Swift编程中,如何裁剪图像并将其放在中心之后?
这就是我到目前为止所做的...我已成功裁剪图像,但我想把它放在中心之后
ImgView.image = OrigImage
var masklayer = CAShapeLayer()
masklayer.frame = ImgView.frame
masklayer.path = path.CGPath
masklayer.fillColor = UIColor.whiteColor().CGColor
masklayer.backgroundColor = UIColor.clearColor().CGColor
ImgView.layer.mask = masklayer
UIGraphicsBeginImageContext(ImgView.bounds.size);
ImgView.layer.renderInContext(UIGraphicsGetCurrentContext())
var image = UIGraphicsGetImageFromCurrentImageContext()
ImgView.image = image
UIGraphicsEndImageContext();
Run Code Online (Sandbox Code Playgroud)
更新:
let rect: CGRect = CGRectMake(path.bounds.minX, path.bounds.minY, path.bounds.width, path.bounds.height)
// Create bitmap image from context using the rect
let imageRef: CGImageRef = CGImageCreateWithImageInRect(image.CGImage, rect)
ImgView.bounds = rect
ImgView.image = UIImage(CGImage: imageRef)
Run Code Online (Sandbox Code Playgroud)
我能够通过获取path.bound和size来中心它并更改我的ImageView的边界.:)
即使应用程序进入后台,我也想播放音乐.我检查了所有stackoverflow链接,但没有一个工作.请帮助今天做.
我使用了以下代码: -
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"Day At The Beach" ofType: @"mp3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
NSError *error;
playerTemp = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:&error];
playerTemp.numberOfLoops = 0;
AudioSessionSetActive(true);
[playerTemp play];
Run Code Online (Sandbox Code Playgroud) 删除所有空格\n以及\rSwift中的String中最有效的方法是什么?
我试过了:
for character in string.characters {
}
Run Code Online (Sandbox Code Playgroud)
但这有点不方便.
我已tableView:indentationLevelForRowAtIndexPath在我的UITableViewController派生类中重写了该方法,如下所示:
- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary* item = [self.projects objectAtIndex:indexPath.row];
int indentationLevel = [[item objectForKey:@"indent"] intValue];
DLog (@"Indentation Level for Row %d : %d", indexPath.row, indentationLevel);
return indentationLevel;
}
Run Code Online (Sandbox Code Playgroud)
我最初认为这没有被调用,但这是操作员错误(错误,我的),我没有定义符号DEBUG = 1.
然而,它被称为(对我!),这是日志输出:
-[RootViewController tableView:indentationLevelForRowAtIndexPath:] [Line 129] Indentation Level for Row 0 : 1
-[RootViewController tableView:indentationLevelForRowAtIndexPath:] [Line 129] Indentation Level for Row 1 : 1
-[RootViewController tableView:indentationLevelForRowAtIndexPath:] [Line 129] Indentation Level for Row 2 : 2
-[RootViewController tableView:indentationLevelForRowAtIndexPath:] [Line 129] Indentation Level for Row …Run Code Online (Sandbox Code Playgroud) 我正在为苹果手表开发一个应用程序.我面临的问题是手表在一段时间后进入睡眠模式.我想以编程方式禁用睡眠模式.
对此有何解决方案?提前致谢!
我创建了自定义TimePicker UIPickerView,取3 UIPickerView.
通过选择时间,我使用该时间来设置本地通知.因为我的应用程序基于警报功能.
UI部分和功能部分在iPhone 5,5s,6,6 Plus中运行良好但在iPhone 6s plus设备中面临问题.没有弄错了!相同的代码适用于除iPhone 6s plus之外的所有其他设备.
下面的截图是iPhone 6,
如您所见,ScreenShot时间和PickerView时间相同.它是功能部分.我已编写代码,捕获当前时间,并根据我设置了PickerView的选定索引.作品完美!此外,我已设置选定的索引字体更大.这在iPhone 6设备中看起来也很完美.
下面的截图是iPhone 6s Plus,
如您所见,ScreenShot的时间和PickerView时间不一样.所以功能部分在iPhone 6s plus设备中不起作用.同样在UI部分中,字体在第二行中对于所选行PickerView而不是在第一行中变大.
此外,当我通过从时间选择器中选择时间来设置本地通知时.除iPhone 6s以外的所有其他设备也能很好地工作(通知会在完美的时间点火).在iPhone 6s加上我选择的任何时间UIPickerView,通知立即生效.
不知道iPhone 6s plus设备出了什么问题.问题是PickerView或其他问题.但是为什么相同的代码适用于所有其他设备,而不仅仅适用于iPhone 6s plus设备.
任何帮助表示赞赏!严重陷入这个问题.如果有人对此问题有任何解决方案,请帮忙!提前致谢.
更新:
请查看我的代码......
AddAlarmViewController.h
@interface AddAlarmViewController : UIViewController <UIPickerViewDelegate,UIPickerViewDataSource>
{
}
@property (strong,nonatomic) NSArray *arrHours;
@property (strong,nonatomic) NSArray *arrMinute;
@property (strong,nonatomic) NSArray *arrAP;
@property (nonatomic,strong) IBOutlet UIPickerView *myPickerHour;
@property (nonatomic,strong) IBOutlet UIPickerView *myPickerMinute;
@property (nonatomic,strong) IBOutlet UIPickerView *myPickerAP;
Run Code Online (Sandbox Code Playgroud)
AddAlarmViewController.m
@interface …Run Code Online (Sandbox Code Playgroud) 我面临一个非常奇怪的问题,因为我有一个在 Xcode 8.3.2 和 swift 3 中创建的项目,现在我用 swift 4 更新了代码并使用最新的 Xcode 9.2 我所做的所有其他更改,应用程序在模拟器和设备
主要问题: - 当我打开故事板以便它正确加载时,如果我在任何视图控制器(接口构建器)中进行任何更改,那么它在退出故事板并重新加载后没有反映在该视图控制器(接口构建器)中,所以我的更改正在显示,所以我对于我在故事板中所做的每一个更改,都必须一直这样做
我试图搜索整个堆栈,还有许多其他链接仍然没有找到解决方案,所以让我知道解决方案或您的建议
注意:- 我不能采用新的故事板,因为我已经设计了太多屏幕。
我正在 Xcode v11 上创建一个 Web 应用程序,但在实现 WKUIDelegate 以显示 Javascript 警报并在 Web 应用程序上正确确认时遇到问题。
我在 ContentView.swift 上有一个非常简单的 webview 应用程序,其中包含以下代码,但不确定如何WKUIDelegate使用此代码正确实现。
import SwiftUI
import WebKit
struct Webview : UIViewRepresentable {
let request: URLRequest
var webview: WKWebView?
init(web: WKWebView?, req: URLRequest) {
self.webview = WKWebView()
self.request = req
}
func makeUIView(context: Context) -> WKWebView {
return webview!
}
func updateUIView(_ uiView: WKWebView, context: Context) {
uiView.load(request)
}
func goBack(){
webview?.goBack()
}
func goForward(){
webview?.goForward()
}
func reload(){
webview?.reload()
}
}
struct ContentView: View …Run Code Online (Sandbox Code Playgroud) 我正在播放一个简短的wav文件AVAudioPlayer.声音每隔几秒钟从一个NSTimer方法重放一次.第一次播放声音时,音量很好,但在随后播放时,音量很低,几乎听不见.在第一场比赛中途偶尔会出现音量下降.这种情况发生在iPad 3上,因此扬声器的选择不是问题.
NSError *phoneCallError;
NSURL *phoneCallUrl = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/phone-calling-1.wav", [[NSBundle mainBundle] resourcePath]]];
AVAudioPlayer *phoneCallPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:phoneCallUrl error:&phoneCallError];
if (phoneCallError) {
NSLog(@"%@: %@", self, phoneCallError);
} else {
phoneCallPlayer.numberOfLoops = 0;
}
Run Code Online (Sandbox Code Playgroud)
该NSTimer方法只是调用[phoneCallPlayer play].在任何时候都无法以编程方式设置音量.有什么建议?谢谢.
我有一个TableView里面的ViewController. 我TableView拉伸了 View SuperMargings(有约束)并禁用了所有SafeAreaInset 选项,但是当我运行我的项目时,我TableView仍然在SafeArea。
我怎样才能让我TableView在 iPhone 上全高带缺口?
swift ×5
objective-c ×4
ios ×3
iphone ×3
apple-watch ×1
ios9.3 ×1
ipad ×1
safeareaview ×1
storyboard ×1
string ×1
swiftui ×1
tableview ×1
uiedgeinsets ×1
uipickerview ×1
uitableview ×1
watchkit ×1
wkwebview ×1
xcode ×1